How can I index a matrix with only one number in Matlab inside a loop?

4 views (last 30 days)
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!

Accepted Answer

Fangjun Jiang
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
Andre L on 20 Aug 2011
Fangjun, Thanks! I really appreciate your reply and I think it will work in my problem. But I never used more than 2 indices in Matlab...and this is the problem with your solution...i've tried in almost any ways to implement it in my program, but none of these works.
This is what I want to do in the program:
After the i-j loop, in the k-th step, a 19x73 matrix should be associated whith this k, but the output is always the last k only. Am I indexing correcly, with (i,j) or it should be (k,i,j) ?
  1 Comment
Fangjun Jiang
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.

Sign in to comment.


Andre L
Andre L on 20 Aug 2011
Hello! This is my code:
function x = irrad_anual_total;
clc
[H_betam,Kt,Kd,H_mensal,Hd_mensal,H_0,lat,delta,dia,ang_hor] = irrad_incl;
ang_hor = 0;
refl = 0.2;
for k = 1:12
costetaZS(k) = sind(delta(k))*sind(lat)+cosd(delta(k))*cosd(lat)*cosd(ang_hor);
B_0(k) = 1000*(H_mensal(k)-Hd_mensal(k));
B(k) = B_0(k)/costetaZS(k);
for i = 1:19
for j = 1:73
beta = 5*i-5;
azim = -185+5*j;
costetaS(k) = sind(delta(k))*sind(lat)*cosd(beta)-... sind(delta(k))*cosd(lat)*sind(beta)*cosd(azim)+cosd(delta(k))... *cosd(lat)*cosd(beta)*cosd(ang_hor)+cosd(delta(k))*sind(lat)...
*sind(beta)*cosd(azim)*cosd(ang_hor)+cosd(delta(k))*...
sind(azim)*sind(ang_hor)*sind(beta);
Bdir(i,j) = B(k)*max(0,costetaS(k));
Ddif(i,j) = 1000*0.5*Hd_mensal(k)*(1+cosd(beta));
Ralb(i,j) = 1000*0.5*refl*H_mensal(k)*(1-cosd(beta));
Total(i,j) = Bdir(i,j)+Ddif(i,j)+Ralb(i,j);
end
end
T(k) = Total(i,j) %Don't worked
end
.
My goal is to obtain the sum of the 12 matrices and get at the end a 19x73 matrix named Total.
Thanks again Fangjun for your reply!
  4 Comments
Fangjun Jiang
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
Andre L on 21 Aug 2011
Wow! It worked haha!
Thank you infinitely Fangjun!
This is a program to analyse the average solar radiation at any inclination and orientation relative to north-south. You just help to save the humanity with solar power man haha!
I will study the n-D array and cell array that you reply on the first time, I think it will be useful.
Thanks again.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!