如何在R中创建没有列和行索引的矩阵?
要创建没有列和行索引的矩阵,我们首先需要创建矩阵,然后可以使用prmatrix函数来转换没有列和行索引的矩阵,但是我们需要提供函数内部的行数。例如,如果我们有一个包含5行5列的矩阵M,则可以使用prmatrix(M,rowlab = rep(“,” 5),collab = rep(“”)将其转换为没有列和行索引的矩阵,5))。
示例
M1<-matrix(rnorm(25),ncol=5)M1
输出结果
[,1] [,2] [,3] [,4] [,5][1,] -0.3524287 0.4753824 -0.7817841 0.4452398 1.5483299
[2,] 0.7659213 0.8160877 -0.9124624 0.3859241 -0.3645244
[3,] -0.7511866 0.4664604 -2.3584599 0.2926155 0.0354159
[4,] -0.2477745 -0.7132980 -0.6503972 -0.3519619 -0.7805707
[5,] 0.5781549 -0.1417311 -0.4956891 1.1003766 0.2343090
示例
prmatrix(M1,rowlab=rep("",5),collab=rep("",5))
输出结果
-0.3524287 0.4753824 -0.7817841 0.4452398 1.54832990.7659213 0.8160877 -0.9124624 0.3859241 -0.3645244
-0.7511866 0.4664604 -2.3584599 0.2926155 0.0354159
-0.2477745 -0.7132980 -0.6503972 -0.3519619 -0.7805707
0.5781549 -0.1417311 -0.4956891 1.1003766 0.2343090
示例
M2<-matrix(rpois(81,2),ncol=9)M2
输出结果
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9][1,] 3 1 3 2 0 2 0 2 4
[2,] 3 0 1 4 0 3 2 1 2
[3,] 4 3 2 1 4 3 3 3 4
[4,] 0 2 3 3 1 2 2 3 4
[5,] 1 1 2 3 2 1 2 4 1
[6,] 2 3 0 1 1 2 4 3 2
[7,] 6 3 0 1 2 0 1 1 1
[8,] 6 3 3 2 5 1 0 0 0
[9,] 4 1 2 3 2 2 0 1 3
示例
prmatrix(M2,rowlab=rep("",9),collab=rep("",9))
输出结果
3 1 3 2 0 2 0 2 43 0 1 4 0 3 2 1 2
4 3 2 1 4 3 3 3 4
0 2 3 3 1 2 2 3 4
1 1 2 3 2 1 2 4 1
2 3 0 1 1 2 4 3 2
6 3 0 1 2 0 1 1 1
6 3 3 2 5 1 0 0 0
4 1 2 3 2 2 0 1 3
示例
M3<-matrix(rexp(36,1.27),ncol=2)M3
输出结果
[,1] [,2][1,] 1.90172301 0.17485982
[2,] 0.72992558 1.22893098
[3,] 0.34723400 0.03796494
[4,] 0.03941449 1.95001158
[5,] 1.04126572 1.14392629
[6,] 0.25201154 0.57012496
[7,] 1.30187720 0.96230866
[8,] 1.13920057 1.40368354
[9,] 1.41180402 0.12139199
[10,] 0.53137688 0.01768795
[11,] 0.05370309 1.69081321
[12,] 1.13580395 0.11810967
[13,] 0.56185639 1.44204980
[14,] 1.51025894 0.23696430
[15,] 1.26819806 0.07203026
[16,] 0.30501141 0.14618651
[17,] 0.93060957 0.07317170
[18,] 0.92067027 0.08642117
示例
prmatrix(M3,rowlab=rep("",18),collab=rep("",18))
输出结果
1.90172301 0.174859820.72992558 1.22893098
0.34723400 0.03796494
0.03941449 1.95001158
1.04126572 1.14392629
0.25201154 0.57012496
1.30187720 0.96230866
1.13920057 1.40368354
1.41180402 0.12139199
0.53137688 0.01768795
0.05370309 1.69081321
1.13580395 0.11810967
0.56185639 1.44204980
1.51025894 0.23696430
1.26819806 0.07203026
0.30501141 0.14618651
0.93060957 0.07317170
0.92067027 0.08642117
示例
M4<-matrix(runif(40,2,5),nrow=20)M4
输出结果
[,1] [,2][1,] 2.884068 2.732077
[2,] 3.694684 2.517772
[3,] 4.165912 3.924986
[4,] 3.752682 2.414989
[5,] 3.199349 4.666055
[6,] 2.365035 3.800645
[7,] 3.121915 2.395140
[8,] 2.147690 2.463121
[9,] 4.697216 4.965934
[10,] 2.094644 4.681206
[11,] 4.071888 2.360031
[12,] 3.047279 2.485864
[13,] 2.921051 4.556291
[14,] 4.655183 4.367924
[15,] 4.727901 3.647091
[16,] 4.807526 4.086226
[17,] 2.193533 2.246970
[18,] 3.198273 3.197613
[19,] 2.371362 3.260453
[20,] 4.070255 3.981377
示例
prmatrix(M4,rowlab=rep("",20),collab=rep("",20))
输出结果
2.884068 2.7320773.694684 2.517772
4.165912 3.924986
3.752682 2.414989
3.199349 4.666055
2.365035 3.800645
3.121915 2.395140
2.147690 2.463121
4.697216 4.965934
2.094644 4.681206
4.071888 2.360031
3.047279 2.485864
2.921051 4.556291
4.655183 4.367924
4.727901 3.647091
4.807526 4.086226
2.193533 2.246970
3.198273 3.197613
2.371362 3.260453
4.070255 3.981377
以上是 如何在R中创建没有列和行索引的矩阵? 的全部内容, 来源链接: utcz.com/z/326429.html