使用循环从矩阵中创建向量

时间:2021-08-07 15:31:34

Im trying to create a vector by combining multiple matrixes using a loop. If I do it manually it looks like this:

我试图通过使用循环组合多个矩阵来创建矢量。如果我手动执行它看起来像这样:

vector = c(
           matrix(labels[1],ccl$size[1]), 
           matrix(labels[2],ccl$size[2]), 
           matrix(labels[3],ccl$size[3]),
           matrix(labels[4],ccl$size[4]),
           matrix(labels[5],ccl$size[5]))

labels is a vector with a given number of elements, as is ccl$size. the problem is that no loop seems to accept any substring of the function as a valuable input.

labels是具有给定数量元素的向量,ccl $ size也是如此。问题是没有循环似乎接受函数的任何子串作为有价值的输入。

edit: I tried

编辑:我试过了

c(for(i in repeats) 
{matrix(labels[i],ccl$size[i]),}
)

edit2:

EDIT2:

inputs labels: c(2,1,3)

输入标签:c(2,1,3)

ccl$size: c(12,10,7)

ccl $ size:c(12,10,7)

desired output c(2,2,2,2,2,2,2,2,2,2,2,2,1,1,1,1,1,1,1,1,1,1,3,3,3,3,3,3,3)

期望输出c(2,2,2,2,2,2,2,2,2,2,2,2,1,1,1,1,1,1,1,1,1,1,3, 3,3,3,3,3,3)

1 个解决方案

#1


2  

You're looking for rep:

你正在寻找代表:

v1 <- c(2,1,3)
v2 <- c(12, 10, 7)

rep(v1, v2)
#  [1] 2 2 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 3 3 3 3 3 3 3

#1


2  

You're looking for rep:

你正在寻找代表:

v1 <- c(2,1,3)
v2 <- c(12, 10, 7)

rep(v1, v2)
#  [1] 2 2 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 3 3 3 3 3 3 3