How can I index a matrix with only one number in Matlab inside a loop?
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
Kishor
on 20 Aug 2011
give example clearly.Check out what is cell array. use it
Accepted Answer
More Answers (2)
Andre L
on 20 Aug 2011
0 votes
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
Still not executable for others. We don't have irrad_incl.
Are you saying that you have 12 matrices, each is 19x73 and you want to add them together and get one 19x73 matrix? It doesn't sound like you need a 3-D matrix. You can always try on a small example to give you some ideas.
total=zeros(2,3)
for k=1:3
total=total+rand(2,3)
end
Andre L
on 21 Aug 2011
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;
Andre L
on 21 Aug 2011
Categories
Find more on Loops and Conditional Statements 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!