复制矩阵到表单列表 [英] Duplicate matrix to form list
本文介绍了复制矩阵到表单列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
a是一个矩阵.
a<-matrix(c(2,2,1,1,1,2,1,2,1,1,2,1,1,1,1,1,1,2,2,2,1,2,1,1),6)
我想将矩阵"a"重复3次以形成一个列表.
I want to duplicate matrix "a" 3 times to form a list.
我尝试过:
as.list(rep(a,3))
但是它不起作用. 我的预期结果如下:
but it doesn't work. My expected result is as follows:
[[1]]
[,1] [,2] [,3] [,4]
[1,] 2 1 1 2
[2,] 2 2 1 2
[3,] 1 1 1 1
[4,] 1 1 1 2
[5,] 1 2 1 1
[6,] 2 1 2 1
[[2]]
[,1] [,2] [,3] [,4]
[1,] 2 1 1 2
[2,] 2 2 1 2
[3,] 1 1 1 1
[4,] 1 1 1 2
[5,] 1 2 1 1
[6,] 2 1 2 1
[[3]]
[,1] [,2] [,3] [,4]
[1,] 2 1 1 2
[2,] 2 2 1 2
[3,] 1 1 1 1
[4,] 1 1 1 2
[5,] 1 2 1 1
[6,] 2 1 2 1
推荐答案
rep
返回与接收到的相同类型,因此,如果将数值矩阵传递给它,它将尝试返回某种形式的数值向量-而不是一个列表.不过,解决方案很简单:如果您想要一个列表,请将其传递给列表:
rep
returns the same type it receives, so if you pass it a numeric matrix, it's going to try to return a numeric vector of some sort–not a list. The solution, though, is simple: if you want a list, pass it a list:
rep(list(a), 3)
# [[1]]
# [,1] [,2] [,3] [,4]
# [1,] 2 1 1 2
# [2,] 2 2 1 2
# [3,] 1 1 1 1
# [4,] 1 1 1 2
# [5,] 1 2 1 1
# [6,] 2 1 2 1
#
# [[2]]
# [,1] [,2] [,3] [,4]
# [1,] 2 1 1 2
# [2,] 2 2 1 2
# [3,] 1 1 1 1
# [4,] 1 1 1 2
# [5,] 1 2 1 1
# [6,] 2 1 2 1
#
# [[3]]
# [,1] [,2] [,3] [,4]
# [1,] 2 1 1 2
# [2,] 2 2 1 2
# [3,] 1 1 1 1
# [4,] 1 1 1 2
# [5,] 1 2 1 1
# [6,] 2 1 2 1
这篇关于复制矩阵到表单列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文