矩阵列表
偶尔,我写了一些错误的R代码,它产生一个对象,就像一个矩阵一样,但元素是向量的元素是 print
。它可能看起来像这样:
Occasionally, I've written buggy R code that produced an object that print
ed like a matrix, but where the elements were vectors. It might look something like this:
[,1] [,2] [,3]
[1,] character,2 character,2 character,2
[2,] character,2 character,2 character,2
[3,] character,2 character,2 character,2
我从来没有想要先创建一个,但现在我正在开展一个项目,这个数据结构可能很有用。
I've never intended to create one before, but now I'm working on a project where this data structure could be useful.
是什么?我如何做一个?它的属性是什么?为了迭代行和列,它的效率如何与嵌套列表,数组或矩阵列表进行比较?
What is it? How do I make one? What are its properties? For iterating over rows and columns, how does its efficiency compare to a nested list, an array, or a list of matrices?
Making one is fairly trivial:
mtx <- matrix( list(letters[1:2]), 4,4)
mtx
#----------
[,1] [,2] [,3] [,4]
[1,] Character,2 Character,2 Character,2 Character,2
[2,] Character,2 Character,2 Character,2 Character,2
[3,] Character,2 Character,2 Character,2 Character,2
[4,] Character,2 Character,2 Character,2 Character,2
属性(和检索它的函数)被命名为dim。我不会指望它在访问效率方面有很大的不同。 R中的矩阵实际上只是折叠向量。
The attribute (and the function that retrieves it) is named "dim". I would not expect it to be very much different in terms of access efficiency. Matrices in R are really just folded vectors.