如何存储在1个可变多个矩阵,这样我可以进行计算?

问题描述 投票:0回答:1

我希望能够存储许多矩阵下1个变量,然后依次用其他矩阵mulitply他们。我想到了一个名单将做的工作,但它引起了我的一些问题。

这是我的输入

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个回答
0
投票

当你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后阅读理解索引运营商之间的差异。

© www.soinside.com 2019 - 2024. All rights reserved.