Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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)

Some how my code does not seem to be correct. Any help would be highly appreciated. Many thanks in advanced. Best.

>Solution :

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

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

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading