如何找到R中矩阵的每一列的最大值?
为了找到矩阵每一列的最大值,我们需要使用apply函数。例如,如果我们有一个包含2行和2列的矩阵M,第一行的值为1、2,第二行的值为3、4,则可以通过使用来找到该矩阵中每一列的最大值语法;apply(M,2,max),因此结果将为3,4。
示例
M1−-matrix(1:36,ncol=6) M1
输出结果
[,1] [,2] [,3] [,4] [,5] [,6] [1,] 1 7 13 19 25 31 [2,] 2 8 14 20 26 32 [3,] 3 9 15 21 27 33 [4,] 4 10 16 22 28 34 [5,] 5 11 17 23 29 35 [6,] 6 12 18 24 30 36
示例
apply(M1,2,max)
输出结果
[1] 6 12 18 24 30 36
示例
M2<-matrix(1:100,ncol=10) M2
输出结果
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] 1 11 21 31 41 51 61 71 81 91 [2,] 2 12 22 32 42 52 62 72 82 92 [3,] 3 13 23 33 43 53 63 73 83 93 [4,] 4 14 24 34 44 54 64 74 84 94 [5,] 5 15 25 35 45 55 65 75 85 95 [6,] 6 16 26 36 46 56 66 76 86 96 [7,] 7 17 27 37 47 57 67 77 87 97 [8,] 8 18 28 38 48 58 68 78 88 98 [9,] 9 19 29 39 49 59 69 79 89 99 [10,] 10 20 30 40 50 60 70 80 90 100
示例
apply(M2,2,max)
输出结果
[1] 10 20 30 40 50 60 70 80 90 100
示例
M3<-matrix(sample(rnorm(10),25,replace=TRUE),ncol=5) M3
输出结果
[,1] [,2] [,3] [,4] [,5] [1,] -0.8410610 -0.8410610 -1.1271937 -0.8410610 -0.1262492 [2,] -1.1271937 -0.3367099 -0.8410610 -0.2109494 -0.2109494 [3,] -0.1262492 -1.0366269 -0.2109494 -0.3367099 -0.2109494 [4,] -0.2109494 -0.3367099 -0.3367099 -0.3367099 -1.9793046 [5,] -1.2250289 -0.1262492 -0.1262492 -1.0366269 -1.1271937
示例
apply(M3,2,max)
输出结果
[1] -0.1262492 -0.1262492 -0.1262492 -0.2109494 -0.1262492
示例
M4<-matrix(sample(rnorm(100,10,1),25),ncol=5) M4
输出结果
[,1] [,2] [,3] [,4] [,5] [1,] 8.880632 9.932590 12.152003 10.745158 12.644492 [2,] 8.743728 9.223513 9.937427 9.327102 9.490583 [3,] 7.502350 10.231251 9.350336 10.823048 9.282358 [4,] 9.830723 9.045965 10.197617 9.194218 9.420975 [5,] 9.974345 10.835077 9.982087 10.112384 9.404278
示例
apply(M4,2,max)
输出结果
[1] 9.974345 10.835077 12.152003 10.823048 12.644492
示例
M5<-matrix(sample(rpois(100,10),64),ncol=8) M5
输出结果
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [1,] 16 4 9 9 10 5 15 9 [2,] 3 6 11 6 15 17 5 9 [3,] 7 10 9 6 16 14 6 2 [4,] 12 10 9 5 10 9 7 11 [5,] 11 9 10 9 12 10 17 6 [6,] 11 10 7 10 9 12 8 9 [7,] 11 7 6 11 8 9 9 8 [8,] 9 12 12 9 12 12 14 12
示例
apply(M5,2,max)
输出结果
[1] 16 12 12 11 16 17 17 12
示例
M6<-matrix(sample(1:10,100,replace=TRUE),ncol=10) M6
输出结果
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] 7 1 7 3 5 7 8 7 10 4 [2,] 6 9 7 4 6 5 5 10 9 7 [3,] 10 4 2 2 8 9 9 4 4 1 [4,] 3 1 7 3 3 7 10 8 7 4 [5,] 5 10 7 8 10 3 5 1 8 4 [6,] 7 3 10 4 5 1 5 5 3 4 [7,] 6 4 4 2 1 9 3 6 6 2 [8,] 10 4 2 8 6 10 6 2 5 8 [9,] 6 9 10 7 3 4 6 6 5 4 [10,] 7 10 5 3 6 7 3 8 9 2
示例
apply(M6,2,max)
输出结果
[1] 10 10 10 8 10 10 10 10 10 8
示例
M7<-matrix(sample(runif(10,2,5),25,replace=TRUE),ncol=5) M7
输出结果
[,1] [,2] [,3] [,4] [,5] [1,] 2.637646 4.744968 3.122147 4.563885 4.744968 [2,] 3.418107 2.637646 3.331060 3.122147 2.845479 [3,] 2.190630 3.331060 2.637646 2.190630 4.834246 [4,] 4.834246 4.563885 2.845479 2.845479 3.418107 [5,] 2.845479 2.190630 2.190630 4.744968 2.637646
示例
apply(M7,2,max)
输出结果
[1] 4.834246 4.744968 3.331060 4.744968 4.834246
示例
M8<-matrix(sample(5001:9999,64),ncol=8) M8
输出结果
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [1,] 5294 5567 9539 5907 9808 6692 6810 9809 [2,] 9005 5150 9502 7604 8849 8504 6631 6261 [3,] 6694 5162 7813 5995 9684 8647 7094 9828 [4,] 5055 7450 7588 8006 8480 5145 9741 8959 [5,] 8300 5964 8411 6109 5320 8649 5777 9473 [6,] 9054 5585 7304 6827 6231 5199 8518 8958 [7,] 5912 8356 6741 9949 6363 9488 7093 6813 [8,] 6312 7701 5741 6947 6012 6579 6873 7793
示例
apply(M8,2,max)
输出结果
[1] 9054 8356 9539 9949 9808 9488 9741 9828