How can I index a matrix with only one number in Matlab inside a loop?
4 views (last 30 days)
Show older comments
Hello!
I'm new in the forum and I would like a little help... I'm facing the following problem: I have a loop variable k that goes from 1 to 12, and in each step k it has associated with it a matrix.
I would like to do something like this: x(1) = Matrix1, ...,x(k) = Matrixk, but I can't...Matlab says "??? In an assignment A(I) = B, the number of elements in B and I must be the same."
Each k has a 19x73 matrix associated, so it will be useful to find a way to do this indexing...haha, meanwhile the program has 12 blocks just for do this job... Any help is appreciated. Thanks!
1 Comment
Accepted Answer
Fangjun Jiang
on 20 Aug 2011
Use n-D array or cell array
%n-D array
x=zeros(12,19,73)
for k=1:12
x(k,:,:)=rand(19,73);
end
% cell array
x=cell(12,1);
for k=1:12
x{k}=rand(19,73);
end
More Answers (2)
Andre L
on 20 Aug 2011
1 Comment
Fangjun Jiang
on 20 Aug 2011
Your code is not executable with missing variables. You can just copy and paste all your code here. Remember to apply the {}code format to it.
Andre L
on 20 Aug 2011
4 Comments
Fangjun Jiang
on 21 Aug 2011
Okay! If you just need the sum "T{1}+T{2}+...+T{12}", not the individual T{1}, T{2} to T{12}, you can utilize array to save memory.
Right before for k=1:12 line, add T=zeros(19,73)
Replace "T(k) = Total(i,j) %Don't worked" line with T=T+Total;
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!