2

我有一个带有以下表格的列表:

x = replicate(3, list(m = matrix(rnorm(16), 4, 4), s = rnorm(1)), simplify=FALSE)
x
#[[1]]
#[[1]]$m
#          [,1]     [,2]     [,3]     [,4]
#[1,] -0.412209 -0.50227 -2.62340  0.17549
#[2,]  1.344723 -1.52060 -0.59093  1.19701
#[3,] -0.016289 -0.88286 -0.33307 -0.48351
#[4,]  0.060760  0.32131  1.16896 -0.45940
#
#[[1]]$s
#[1] -1.3588
#
#
#[[2]]
#[[2]]$m
#         [,1]     [,2]     [,3]    [,4]
#[1,] -0.15345 -0.82106  0.93839 1.46115
#[2,]  1.60950 -2.35833  0.89621 0.35542
#[3,] -0.17792  0.54444 -0.17414 0.63614
#[4,]  0.85038  0.84373  0.24687 0.60172
#
#[[2]]$s
#[1] 3.6597
#
#
#[[3]]
#[[3]]$m
#         [,1]     [,2]     [,3]     [,4]
#[1,]  1.27724  0.19397  0.47393 -0.66518
#[2,] -0.75244  1.16476  1.74843 -1.03732
#[3,]  0.99433 -1.06129  0.15674 -0.83856
#[4,]  0.18471  1.30852 -0.38024  0.88538
#
#[[3]]$s
#[1] 0.49526

目标是从上面的列表中获取矩阵列表,以及标量向量。如何才能做到这一点?谢谢!

4

1 回答 1

4

你的意思是这样的……?

sapply(x, '[', 'm')         # List of matrices
unlist(sapply(x, '[', 's')) # Vector of scalars
于 2013-05-20T14:31:31.250 回答