Matrix indexing within a loop

1 view (last 30 days)
Giuseppe Brandi
Giuseppe Brandi on 4 Dec 2014
Commented: dpb on 4 Dec 2014
Hi,
I am a newbie on Matlab and I am trying to solve the following problem:
I have N(with N very large) consecutive matricies( A1,A2,A3,....,AN). Every matrix has the first 2 columns with an id and the 3rd collumn with a number(matricies are Mx3, with M varying). I have also a matrix AA which contains all the possible combination of the first 2 columns of the A1,..,AN matricies (all possible combination of ids).
I need to do the following thing: for a K number of matrix( with K<N) I need to evaluate this:
in a loop which goes from 1 to lenght(Ai) (j=1:lenght(Ai)) for the Ai matrix, and from 1 to length(AA) (h=1: lenght(AA)), if the first two columns of Ai (with i= 1:K) == the first two columns of AA then, at AA2(h,:)=[AA(h,1:2),(AA(h,3)+Ai(j,3))] and I would like that this can be done for each i(at each loop, it has to evaluate the matrix A1, then A2 up to AK and sum it to the 3rd column of AA if the condition is satisfied. The problem is that I am not able to recall the matricies Ai. I tried with A{i} but it doesn't work.
This is the code I am trying to implement:
for i=1:length(A1)
for j=1:length(AA1(:,1))
if AA1(j,1:2)==A1(i,1:2)
AA2(j,:)=B+[AA1(j,1:2),(AA1(j,3)+A1(i,3))];
end
end
j=j+1;
end
i=i+1;
but what I need is something like this:
for k=1:15
for i=1:length(Ak)
for j=1:length(AA1(:,1))
if AA1(j,1:2)==Ak(i,1:2)
AA2(j,:)=B+[AA1(j,1:2),(AA1(j,3)+Ak(i,3))];
end
end
j=j+1;
end
i=i+1;
end
k=k+1;
I don't know if I can put this matricies in a bigger matrix or table and the recall them from there or other thing. Any suggestion and help would be really appreciated.
Thank you in advance. Best,
Giuseppe
  1 Comment
dpb
dpb on 4 Dec 2014
I have N(with N very large) consecutive matricies( A1,A2,A3,....,AN)...
And there is your first problem. Without reading further you've created a nightmare. See
for alternatives and why using naming instead is a bad idea (actually, you've already discovered the "why" part... :) )

Sign in to comment.

Answers (0)

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!