Matrix indexing within a loop
조회 수: 12 (최근 30일)
이전 댓글 표시
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
dpb
2014년 12월 4일
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... :) )
답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!