我有一个浮点值的3D矩阵,我需要里面的最大值和最小值,如:
[[1]]
0.1 0.3 0.6 0.8
0.09 0.1 0.4 0.6
[[2]]
0.08 0.2 0.5 0.75
0.01 0.3 0.5 0.55
生成者:
m <- list(matrix(c(0.1,0.3,0.6,0.8,
0.09,0.1,0.4,0.6),2,4,byrow=TRUE),
matrix(c(0.08,0.2,0.5,0.75,
0.01,0.3,0.5,0.55),2,4,byrow=TRUE))
max为0.8且min 0.01
我尝试使用max()和min()函数但不起作用..
答案 0 :(得分:2)
试试这个:
> m1<-matrix(runif(8,1,100),2,4)
> m1
[,1] [,2] [,3] [,4]
[1,] 98.88260 97.13486 2.712315 35.09616
[2,] 43.90468 99.59167 41.873303 94.19843
> m2<-matrix(runif(8,1,100),2,4)
> m2
[,1] [,2] [,3] [,4]
[1,] 26.61785 61.11184 42.28846 55.87778
[2,] 49.27263 59.28607 46.72659 76.24321
> l<-list(m1,m2)
> lapply(l,FUN = max)
[[1]]
[1] 99.59167
[[2]]
[1] 76.24321
> lapply(l,FUN = min)
[[1]]
[1] 2.712315
[[2]]
[1] 26.61785
此外,您可以同时使用一行代码:lapply(l,FUN = range)
(感谢Joseph Wood提出的最后建议。请参阅评论)