Index with for loop variable name and definition

I have a matrix A and avector y. I want to create a loop which names and defines them the matrix A from A1,...,A10.
for i = 1:10
A{i} = X(y==i)
end
Unfortunately this code does not work. It would be nice to end up with a double and not a cell

댓글 수: 1

Stephen23
Stephen23 2018년 6월 29일
편집: Stephen23 2018년 6월 29일
"I want to create a loop which names and defines them the matrix A from A1,...,A10."
Dynamically accessing variable names is one way that beginners force themselves into writing slow, complex buggy, code. Read this to know why:
Using a cell array with indexing is simple, neat, and very efficient. You should use a cell array with indexing.
Note that when you put a number into the variable name like that then you are using it as pseudo-indexing: note that processing pseudo-indexing is complex and slow. In contrast, real indexing of a cell array (as your question shows) is extremely fast, efficient, and easy to debug.
So far you have not provided any reason why you need to write much slower, more complex, buggier code. Cell arrays are designed to efficiently hold data arrays, exactly like you want to now.

댓글을 달려면 로그인하십시오.

 채택된 답변

D. Plotnick
D. Plotnick 2018년 6월 29일

1 개 추천

If I understand your question, it is parallel to the one here, which as Stephen Cobeldick points out is a bad idea.
There is sort of a way around this using a structure.
for i = 1:10
fname = ['A',num2str(i)];
B.(fname) = y(i);
end

댓글 수: 5

Stephen23
Stephen23 2018년 6월 29일
편집: Stephen23 2018년 6월 29일
"which as Stephen Cobeldick points out is a bad idea."
I did not not write the MATLAB documentation which advises against this:
I did not write the thousands of threads where expert MATLAB users advise against doing this:
I did not write the MATLAB wiki page:
I am not a member of TMW staff who advises against doing this:
@Daniel Plotnick: please do not describe me as the source of this advice, as it distracts from the real source of this information: the writers of the MATLAB documentation, and MATLAB users with much more knowledge than I have. It would be appreciated if you referred to the actual sources in your future comments.
Stef
Stef 2018년 6월 29일
Ok, say I create the variables A1 to A10 manually. Is there a possibility to create a loop through the index of A, so that I can choose the A I want through its name in a loop?
Stephen23
Stephen23 2018년 6월 29일
편집: Stephen23 2018년 6월 29일
"Is there a possibility to create a loop through the index of A, so that I can choose the A I want through its name in a loop?"
If you really want to write slow, complex, buggy code then the links in my comments ( here and here) reference how you can do this.
Note that both creating variable names dynamically and accessing variable names dynamically suffers exactly the same problems: slow, inefficient, more liable to bugs, much harder to debug. Also note that once you start defining/accessing variable names dynamically then you force yourself into writing the rest of your code inefficiently, because for the rest of the code you will have to use the same slow, complex, buggy ways to access your data.
What is the problem with using a cell array (which is simple, efficient, easy to debug, etc.)?
Stef
Stef 2018년 6월 29일
Thanks, it worked!
Stephen23
Stephen23 2018년 6월 29일
편집: Stephen23 2018년 6월 29일
@Stef: note that your original code, using a cell array and indexing, is faster and simpler than this answer.
You might also find this to be of interest:

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

질문:

2018년 6월 28일

편집:

2018년 6월 29일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by