[英]Extracting Matrix from List of Matrices
Say I have a list of 3 Matrices.假设我有一个包含 3 个矩阵的列表。 How do I extract one of the matrices as a Matrix-Object, so I can further access it's elements with
mymatrix[i,j]
calls?如何将其中一个矩阵提取为矩阵对象,以便我可以通过
mymatrix[i,j]
调用进一步访问它的元素?
mylist[i]
simply returns a List-Object with Matrix i
. mylist[i]
简单地返回一个带有矩阵i
的列表对象。
I'm sure there has to be a simple solution, but I couldn't manage to find one.我确信必须有一个简单的解决方案,但我无法找到一个。
use double brackets: mylist[[i]]
....使用双括号:
mylist[[i]]
....
From ?"[["
(emphasis added):从
?"[["
(强调):
Recursive (list-like) objects:
递归(类列表)对象:
Indexing by '[' is similar to atomic vectors and selects a list of the specified element(s) .
'[' 索引类似于原子向量,并选择指定元素的列表。
Both '[[' and '$' select a single element of the list .
'[[' 和 '$' 都选择列表中的一个元素。 The main difference is that '$' does not allow computed indices,
主要区别在于“$”不允许计算索引,
mylist[[1]]
should return you the first matrix mylist[[1]]
应该返回你的第一个矩阵
mylist[[3]]
should return you the third matrix mylist[[3]]
应该返回第三个矩阵
If you want to automate this you could simply run:如果你想自动化这个,你可以简单地运行:
for (index in 1:length(mylist)){
assign(paste0("matrix", index), mylist[[index]], envir = .GlobalEnv)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.