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

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

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)

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

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.

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

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
I got it Fangjun!
What I did was to use the idea of cell array. At the code, I replaced the "T(k) = Total(i,j)" by "T{k} = Total", changing () to {}. In this way, each k at the end of the i-j loop was associated with its matrix, for example, T{11}. Really wasn't needed a 3-D matrix.
The sum is performed as T{1}+...+T{12} (as you can easily see, I am new in programming - by the way, in english too haha). In the end I got the result I needed, but if there are another way to perform this sum it would be great!
Thanks again. This was a important question...indeed I didn't find nothing in internet about indexing a matrix with an escalar like this.
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;
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.

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!