如何找到存储在 R 列表中的所有矩阵的每一行的最大值?

要找到存储在 R 列表中的所有矩阵的每一行的最大值,我们可以按照以下步骤操作 -

  • 首先,创建一个矩阵列表。

  • 然后,使用 max 函数以及 lapply 函数和 apply 函数来找到所有矩阵的每一行的最大值。

创建矩阵列表

使用矩阵函数创建多个矩阵并使用列表函数将它们存储在列表中 -

例子

M1<-matrix(rnorm(20),ncol=2)

M2<-matrix(rnorm(20),ncol=2)

M3<-matrix(rnorm(20),ncol=2)

M4<-matrix(rnorm(20),ncol=2)

M5<-matrix(rnorm(20),ncol=2)

List<-list(M1,M2,M3,M4,M5)

List

执行时,上述脚本生成以下内容output(this output will vary on your system due to randomization)-

输出

[[1]]

         [,1]        [,2]

[1,]  0.01714936  1.3763921

[2,] -1.55809582 -0.7712093

[3,] -0.35186736  0.8351507

[4,] -0.48988731 -1.2714754

[5,] -0.97241718 0.8105862

[6,] -0.03231086 -0.1901989

[7,]  0.34748364 1.5562359

[8,]  1.10587259 -0.6592835

[9,]   0.38934787 1.0586670

[10,] -0.24586360 -2.2442172

[[2]]

          [,1]     [,2]

[1,] -0.6150390 -0.2355494

[2,] -0.1527035  0.8530879

[3,] -0.5910591 -0.0579156

[4,] -0.4871413 -0.9829705

[5,] 2.1435879   0.7445451

[6,] -0.8603206 -0.4894914

[7,] -0.6303660 -0.4026965

[8,] -1.0862919 0.3214481

[9,] 0.5157734 0.6113277

[10,] 1.4241205 -0.4793407

[[3]]

           [,1]    [,2]

[1,] -1.1166732 0.6219046

[2,] 1.2956490 -0.7224109

[3,] 0.4492815 1.0038605

[4,] 1.8768257 -1.4001428

[5,] 0.6893493 -0.3747519

[6,] 0.3261293 1.7425561

[7,] 1.0496563 0.4613786

[8,] -1.1868764 -0.7465893

[9,] 0.2235005 0.9713996

[10,] 0.6216177 -0.5418687

[[4]]

         [,1]       [,2]

[1,] 0.2266899 -2.05779477

[2,] -1.0528427 2.06139961

[3,] -0.5606942 -0.48373582

[4,] 0.2228694 -1.19236303

[5,] 0.7759945 -0.37885960

[6,] 0.4570521 0.13911064

[7,] 0.4439456 -0.08302548

[8,] -0.6691629 1.82642429

[9,] -0.4612115 0.74955822

[10,] 0.1766236 -1.75742835

[[5]]

         [,1]       [,2]

[1,] 0.01010036 -1.54334237

[2,] 2.05466828 -0.11349936

[3,] -0.42566069 0.14698151

[4,] -0.92737532 1.62506335

[5,] -1.09411380 -0.63808937

[6,] 1.55792587 1.48997553

[7,] 1.56339394 -1.16440367

[8,] 0.18405916 -1.17883290

[9,] 0.52923750 0.01279766

[10,] 0.57009101 0.07179523

找到所有矩阵的每一行的最大值

使用 max 函数以及 lapply 函数和 apply 函数来查找存储在 List 中的所有矩阵的每一行的最大值 -

例子

M1<-matrix(rnorm(20),ncol=2)

M2<-matrix(rnorm(20),ncol=2)

M3<-matrix(rnorm(20),ncol=2)

M4<-matrix(rnorm(20),ncol=2)

M5<-matrix(rnorm(20),ncol=2)

List<-list(M1,M2,M3,M4,M5)

lapply(List,FUN=function(x)apply(x,MARGIN=1,FUN=max))

输出

[[1]]

[1] 1.37639206 -0.77120931 0.83515070 -0.48988731 0.81058622 -0.03231086

[7] 1.55623594 1.10587259 1.05866703 -0.24586360

[[2]]

[1] -0.2355494 0.8530879 -0.0579156 -0.4871413 2.1435879 -0.4894914

[7] -0.4026965 0.3214481 0.6113277 1.4241205

[[3]]

[1] 0.6219046 1.2956490 1.0038605 1.8768257 0.6893493 1.7425561

[7] 1.0496563 -0.7465893 0.9713996 0.6216177

[[4]]

[1] 0.2266899 2.0613996 -0.4837358 0.2228694 0.7759945 0.4570521

[7] 0.4439456 1.8264243 0.7495582 0.1766236

[[5]]

[1] 0.01010036 2.05466828 0.14698151 1.62506335 -0.63808937 1.55792587

[7] 1.56339394 0.18405916 0.52923750 0.57009101

以上是 如何找到存储在 R 列表中的所有矩阵的每一行的最大值? 的全部内容, 来源链接: utcz.com/z/311429.html

回到顶部