矩阵列表 [英] Matrix-shaped list

查看:213
本文介绍了矩阵列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

偶尔,我写了一些错误的R代码,它产生一个对象,就像一个矩阵一样,但元素是向量的元素是 print 。它可能看起来像这样:

Occasionally, I've written buggy R code that produced an object that printed 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.

这篇关于矩阵列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆