Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
153 views
in Technique[技术] by (71.8m points)

matrix - How to reference incrementing variables in R?

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

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

1 Reply

0 votes
by (71.8m points)

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]].


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...