简体   繁体   中英

Loop with matrix in R

Suppose,I have a matrix of 10 X 100 as follow (actually matrix quite big):

x = matrix(norm(n=10), 10, 100)

Now I want to make 1000 by 1 matrix by combining column 1 though 100. [for example like rbind .

my code is as follow:

y=matrix(0,nrow=1000,ncol=1) 

for(row in 1:10){
y[1+(row-1)*100:(row)*100, 1]=x[row,1:100] 
}

My expected output is something like this

y=matrix(rnorm(1000),nrow=1000, ncol=1)

Somehow my code does not seem to be correct.

All you need to do is change the dimensions of the matrix:

x <- matrix(1:100, 5, 20)
y <- matrix(x, 100, 1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM