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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With