【问题标题】:How to store multiple matrices under 1 variable so that I can perform calculations?如何在 1 个变量下存储多个矩阵以便我可以执行计算?
【发布时间】:2019-02-04 00:16:32
【问题描述】:

我希望能够在 1 个变量下存储许多矩阵,然后将它们与其他矩阵顺序地相乘。我认为列表可以完成这项工作,但它给我带来了一些问题。

这是我的意见

j = list(matrix(c(0,1,2,3),nrow=2,ncol=2,byrow=TRUE), matrix(c(7,6,5,4),nrow=2,ncol=2,byrow=TRUE))

j[1]
j[1]%*%j[2]
t(j[1])
t(j[1])%*%j[1]

这是我的输出

> j = list(matrix(c(0,1,2,3),nrow=2,ncol=2,byrow=TRUE), matrix(c(7,6,5,4),nrow=2,ncol=2,byrow=TRUE))
> j[1]
[[1]]
     [,1] [,2]
[1,]    0    1
[2,]    2    3

> j[1]%*%j[2]
Error in j[1] %*% j[2] : requires numeric/complex matrix/vector arguments
> t(j[1])
     [,1]     
[1,] Numeric,4
> t(j[1])%*%j[1]
Error in t(j[1]) %*% j[1] : 
  requires numeric/complex matrix/vector arguments

提前致谢。

【问题讨论】:

    标签: r list matrix


    【解决方案1】:

    当您执行j[1] 时,您会得到

    #[[1]]
    #     [,1] [,2]
    #[1,]    0    1
    #[2,]    2    3
    

    这仍然是一个列表。

    class(j[1])
    #[1] "list"
    

    您需要的是 j[[1]],其 class 是矩阵

    class(j[[1]])
    #[1] "matrix"
    
    j[[1]] %*% j[[2]]
    #     [,1] [,2]
    #[1,]    5    4
    #[2,]   29   24
    
    t(j[[1]]) %*% j[[1]]
    #     [,1] [,2]
    #[1,]    4    6
    #[2,]    6   10
    

    我建议通读 this 帖子以了解索引运算符之间的区别。

    【讨论】:

      猜你喜欢
      • 2020-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-17
      • 1970-01-01
      • 1970-01-01
      • 2012-03-01
      • 1970-01-01
      相关资源
      最近更新 更多