2.2 将1,2,…,20构成两个4x5价的矩阵,其中矩阵A按列输入,矩阵B按行输入
(1)A矩阵输入
> A=matrix(1:20,nrow=4,ncol=5,byrow=FALSE)
> A
[,1] [,2] [,3] [,4] [,5]
[1,] 1 5 9 13 17
[2,] 2 6 10 14 18
[3,] 3 7 11 15 19
[4,] 4 8 12 16 20
(2)B矩阵输入
> B=matrix(1:20,nrow=4,ncol=5,byrow=TRUE)
> B
[,1] [,2] [,3] [,4] [,5]
[1,] 1 2 3 4 5
[2,] 6 7 8 9 10
[3,] 11 12 13 14 15
[4,] 16 17 18 19 20
(3)矩阵相加:C=A+B
> C=A+B
> C
[,1] [,2] [,3] [,4] [,5]
[1,] 2 7 12 17 22
[2,] 8 13 18 23 28
[3,] 14 19 24 29 34
[4,] 20 25 30 35 40
(4)矩阵相乘:D=A%*%t(B)
> D=A%*%t(B)
> D
[,1] [,2] [,3] [,4]
[1,] 175 400 625 850
[2,] 190 440 690 940
[3,] 205 480 755 1030
[4,] 220 520 820 1120
(5)矩阵各元素相乘
> E=A*B
> E
[,1] [,2] [,3] [,4] [,5]
[1,] 1 10 27 52 85
[2,] 12 42 80 126 180
[3,] 33 84 143 210 285
[4,] 64 136 216 304 400
(6)A矩阵前3行与前3列构成的矩阵
> A
[,1] [,2] [,3] [,4] [,5]
[1,] 1 5 9 13 17
[2,] 2 6 10 14 18
[3,] 3 7 11 15 19
[4,] 4 8 12 16 20
> F=A[-4,-(4:5)]
> F
[,1] [,2] [,3]
[1,] 1 5 9
[2,] 2 6 10
[3,] 3 7 11
(7)G矩阵由B矩阵构成,但不包括第三列构成新矩阵
> B
[,1] [,2] [,3] [,4] [,5]
[1,] 1 2 3 4 5
[2,] 6 7 8 9 10
[3,] 11 12 13 14 15
[4,] 16 17 18 19 20
> G=cbind(B[,1],B[,2],B[,4],B[,5])
> G
[,1] [,2] [,3] [,4]
[1,] 1 2 4 5
[2,] 6 7 9 10
[3,] 11 12 14 15
[4,] 16 17 19 20
2.3 构造一个向量x,向量是由五个1,三个2,四个3,二个4构成,注意用到rep()函数
> x=rep(1:4,c(5,3,4,2))
> x
[1] 1 1 1 1 1 2 2 2 3 3 3 3 4 4
2.4Hilbert矩阵
(1)构成5阶的Hilbert矩阵
> n <- 5
> hilbert<- array(0, dim = c(n, n))
> for (i in 1:n) {
+ for (j in 1:n) {
+ hilbert[i, j] <-1/(i + j - 1)
+ }
+ }
> hilbert
[,1] [,2] [,3] [,4] [,5]
[1,] 1.0000000 0.5000000 0.3333333 0.2500000 0.2000000
[2,] 0.5000000 0.3333333 0.2500000 0.2000000 0.1666667
[3,] 0.3333333 0.2500000 0.2000000 0.1666667 0.1428571
[4,] 0.2500000 0.2000000 0.1666667 0.1428571 0.1250000
[5,] 0.2000000 0.1666667 0.1428571 0.1250000 0.1111111
(2)求行列式
> det(hilbert)
[1] 3.749295e-12
(3)求逆矩阵
> solve(hilbert)
[,1] [,2] [,3] [,4] [,5]
[1,] 25 -300 1050 -1400 630
[2,] -300 4800 -18900 26880 -12600
[3,] 1050 -18900 79380 -117600 56700
[4,] -1400 26880 -117600 179200 -88200
[5,] 630 -12600 56700 -88200 44100
(4)求特征值和特征向量
> eigen(hilbert)
$values
[1] 1.567051e+00 2.085342e-01 1.140749e-02 3.058980e-04 3.287929e-06
$vectors
[,1] [,2] [,3] [,4] [,5]
[1,] 0.7678547 0.6018715 -0.2142136 0.04716181 0.006173863
[2,] 0.4457911 -0.2759134 0.7241021 -0.43266733 -0.116692747
[3,] 0.3215783 -0.4248766 0.1204533 0.66735044 0.506163658
[4,] 0.2534389 -0.4439030 -0.3095740 0.23302452 -0.767191193
[5,] 0.2098226 -0.4290134 -0.5651934 -0.55759995 0.376245545
2.5已知有5名学生的数据,如表2.3所示,用数据框的形式读入数据
> num = c(1:5)
> name = c("张三", "李四", "王五", "赵六", "丁一")
> sex = c("女", "男", "女", "男", "女")
> age = c(14, 15, 16, 14, 15)
> height = c(156, 165, 157, 162, 159)
> weight = c(42, 49, 41.5, 52, 45.5)
> students = data.frame(num, name, sex, age, height, weight)
> students
num name sex age height weight
1 1 张三 女 14 156 42.0
2 2 李四 男 15 165 49.0
3 3 王五 女 16 157 41.5
4 4 赵六 男 14 162 52.0
5 5 丁一 女 15 159 45.5
2.6将例2.5中的数据写成一个纯文本文件,用函数read.table()读该文件,然后再用函数write.csv()写成能用excel表能打开的文件,并用excel打开。
(1)将数据框中的数据写入到文件students.txt中
write.table(students,"F:\\R\\workspace\\students.txt")
(2)将students.txt的数据读入数据框中
> mydataframe=read.table("students.txt",header=TRUE)
> mydataframe
num name sex age height weight
1 1 张三 女 14 156 42.0
2 2 李四 男 15 165 49.0
3 3 王五 女 16 157 41.5
4 4 赵六 男 14 162 52.0
5 5 丁一 女 15 159 45.5
(3)将数据框中的数据导入到cvs文件中
write.csv(mydataframe,"F:\\R\\workspace\\students.csv")