简体   繁体   English

矩阵列表上的R冒号运算符

[英]R colon operator on list of matrices

I've created a list of matrices in R. In all matrices in the list, I'd like to "pull out" the collection of matrix elements of a particular index. 我在R中创建了一个矩阵列表。在列表的所有矩阵中,我想“拉出”特定索引的矩阵元素集合。 I was thinking that the colon operator might allow me to implement this in one line. 我当时认为冒号运算符可能允许我在一行中实现它。 For example, here's an attempt to access the [1,1] elements of all matrices in a list: 例如,这是尝试访问列表中所有矩阵的[1,1]元素:

myList = list() #list of matrices
myList[[1]] = matrix(1:9, nrow=3, ncol=3, byrow=TRUE) #arbitrary data
myList[[2]] = matrix(2:10, nrow=3, ncol=3, byrow=TRUE)

#I expected the following line to output myList[[1]][1,1], myList[[2]][1,1]
slice = myList[[1:2]][1,1] #prints error: "incorrect number of dimensions"

The final line of the above code throws the error "incorrect number of dimensions." 上面代码的最后一行会引发错误“维数不正确”。

For reference, here's a working (but less elegant) implementation of what I'm trying to do: 作为参考,这是我正在尝试做的工作(但不太优雅)的实现:

#assume myList has already been created (see the code snippet above)
slice = c()
for(x in 1:2) {
    slice = c(slice, myList[[x]][1,1])
}
#this works. slice = [1 2]

Does anyone know how to do the above operation in one line? 有谁知道如何在一行中完成上述操作?

Note that my "list of matrices" could be replaced with something else. 请注意,我的“矩阵列表”可以替换为其他内容。 If someone can suggest an alternative "collection of matrices" data structure that allows me to perform the above operation, then this will be solved. 如果有人可以建议一个允许我执行上述操作的替代“矩阵集合”数据结构,那么这将得到解决。

Perhaps this question is silly...I really would like to have a clean one-line implementation though. 也许这个问题很愚蠢......我真的希望有一个干净的单线实现。

Two things. 两件事情。 First, the difference between [ and [[ . 首先, [[[ The relevant sentence from ?'[' : 来自?'['的相关句子:

The most important distinction between [, [[ and $ is that the [ can select more than one element whereas the other two select a single element. [,[[和$]之间最重要的区别是[可以选择多个元素,而另外两个选择单个元素。

So you probably want to do myList[1:2] . 所以你可能想做myList[1:2] Second, you can't combine subsetting operations in the way you describe. 其次,您无法以您描述的方式组合子集化操作。 Once you do myList[1:2] you will get a list of two matrices. 完成myList[1:2]您将获得两个矩阵的列表。 A list typically has only one dimension, so doing myList[1:2][1,1] is nonsensical in your case. 列表通常只有一个维度,因此在您的情况下执行myList[1:2][1,1]是没有意义的。 (See comments for exceptions.) (参见有关例外情况的评论。)

You might try lapply instead: lapply(myList,'[',1,1) . 你可以尝试lapplylapply(myList,'[',1,1)

If your matrices will all have same dimension, you could store them in a 3-dimensional array. 如果您的矩阵都具有相同的尺寸,您可以将它们存储在三维数组中。 That would certainly make indexing and extracting elements easier ... 这肯定会使索引和提取元素更容易......

## One way to get your data into an array
a <- array(c(myList[[1]], myList[[2]]), dim=c(3,3,2))

## Extract the slice containing the upper left element of each matrix
a[1,1,]
# [1] 1 2

This works: 这有效:

> sapply(myList,"[",1,1)
[1] 1 2

edit: oh, sorry, I see almost the same idea toward the end of an earlier answer. 编辑:哦,对不起,我在早期答案结束时看到几乎相同的想法。 But sapply probably comes closer to what you want, anyway 但无论如何,sapply可能更接近你想要的东西

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM