Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I want to create 200 matrices of dimensions 200 X 129. I have a bit of code that needs to run over the 200 matrices, but each new matrix references the previous one.

for(i in 1:200)
{
  nam <- paste("step", i, sep = "")
  mat<- matrix(ncol=129, nrow=200)
  assign(nam, mat)
  stepg<- matrix(ncol=129, nrow=200)
  stepg<- step[i][200,129]
  index<-sample(1:nrow(stepg), 2)
  }

When I run this code, I get an error "Error in step[i][20, 30] : incorrect number of dimensions". I want to know how to reference the ith matrix.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
151 views
Welcome To Ask or Share your Answers For Others

1 Answer

I'm not altogether sure what the question is asking, but you might find this concept helpful: instead of creating matrices with unique names such as step1 and step2, you can store these objects as elements in a list:

storage_list <- vector(mode="list", length=200)
for(i in 1:200) {
    storage_list[[i]] <- matrix(...)
}

Then you can easily access, e.g., storage_list[[i-1]].


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...