Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save n matrix in loop R

Tags:

loops

r

matrix

I´m just new in R and i would like to create n matrix for loop.

I did a loop to create 3 matrix but i don´t know how to save it.

n=numeric(0)

for (i in 1:3){
  n[i]=5^i
  m=numeric(0)
  m=matrix(data=0,nrow=n[i],ncol=n[i])

  for (j in n[i]:1){
    for (k in 1:i){
     m[j,k]=j+k
    }
  }
}

Anyone could help?

thanks

like image 717
liguang Avatar asked Mar 09 '26 05:03

liguang


1 Answers

Try this by storing matrix into list.

n=numeric(0)
list_mat <- list()
for (i in 1:3){
  n[i]=5^i
  m=numeric(0)
  m=matrix(data=0,nrow=n[i],ncol=n[i])

  for (j in n[i]:1){
    for (k in 1:i){
      m[j,k]=j+k
    }
  }
  list_mat[[i]] <- m #Holding Matrix
}

Output-

> list_mat[[1]]
   [,1] [,2] [,3] [,4] [,5]
[1,]    2    0    0    0    0
[2,]    3    0    0    0    0
[3,]    4    0    0    0    0
[4,]    5    0    0    0    0
[5,]    6    0    0    0    0
like image 199
Rushabh Patel Avatar answered Mar 12 '26 04:03

Rushabh Patel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!