summation of valuse in nested loops

Hello,
I have three nested loop (I change from 1-10, J change from 1-20 and K change from 1-100) I saved the result from J,K loops in matrix (20,100). When loop went to I again I want to sum the old value with new value. For example
new A(1,1)=old A(1,1)+U(j,k) for new I loop.
I used below code and I do not know if it is correct. Please can you suggest new one or correct mine
A(20,100)=0
U(20,100)=0
for i=1:10
for j=1:20
for k=1:100
A(j,k)=A(j,k)+U(j,k)
end
end
end

댓글 수: 2

Star Strider
Star Strider 2015년 10월 23일
Please go into more detail on what you intend with:
new A(1,1)=old A(1,1)+U(j,k) for new I loop.
What should the other elements of ‘A’ be? Do you intend:
new A(l,m)=old A(l,m)+U(j,k) for new I loop?
That could require four nested loops. MATLAB can certainly do that, but there could be more efficient ways than nested loops.
Ali Kareem
Ali Kareem 2015년 10월 23일
편집: Ali Kareem 2015년 10월 24일
Hi,
Thank you for your reply.
I want to save output from equation in matrix A(20,100) this is for loops (J,K). So this matrix A(20,100) will repeated for ten times. each time I want to add the output from old matrix to new one for all cell (each cell will add to same old cell) for all cell in all raw and column.
new A(1,1)=old A(1,1)+U(j,k) ....to .. new A(1,20)=old A(1,20)+U(j,k)
new A(100,1)=old A(100,1)+U(j,k) ... new A(100,20)=old A(100,20)+U(j,k)
A(20,100)=0
U(20,100)=0
z=3
for i=1:10
for j=1:20
for k=1:100
U(j,k)=z^2+sin(j)
A(j,k)=A(j,k)+U(j,k)
end
end
end
for I=1 I will get from (j,k) loops below matrix
A(1,1) A(1,2) A(1,3)…… A(1,100)
A(2,1) A(2,2) A(2,3)…… A(2,100)
A(20,1) A(20,2) A(20,3)…… A(20,100)
When I=2 I want to add each new cell like (A(1,1)) to old cell to new cell ( I mean for I repeated ten times then there will be 10 values for each cell like A(1,1) when I=1, then A(1,1) when I =2….and so on until I=10). I want to have summation for these values. (I mean by old the previous matrix when I=2 the previous matrix is when I =1 and so on until I=10)
A(1,1) A(1,2) A(1,3)…… A(1,100)
A(2,1) A(2,2) A(2,3)…… A(2,100)
Thank you so much A(20,1) A(20,2) A(20,3)…… A(20,100)

댓글을 달려면 로그인하십시오.

답변 (1개)

Andrei Bobrov
Andrei Bobrov 2015년 10월 24일
편집: Andrei Bobrov 2015년 10월 24일

0 개 추천

z = 3;
ii = 10;
jj = 20;
k = 100;
z2 = z^2;
A = (z2+sin(1:jj)'*ones(1,k))*ii;

댓글 수: 6

Hello,
Thank you for your reply. Please I do not think my question was clear. Below what I want to do
for L=1:10
for i=1:20
for j=1:100
r=(Xc+Y)^(1/2);
VC=VCo*(1-(exp(-(r))));
Uf(i,j)=(((VC-Yc))/((pi)*(r)));
Vf(i,j)=((VC*r)*(Xc))/((2*pi));
Uft(i,j)=Uft(i,j)+Uf(i,j);
Vft(i,j)=Vft(i,j)+Vf(i,j);
Ufz(i,j)=(U-Uft(i,j))/U;
end;
end;
end;
I Want to have summation of Uft and Vft. so for the entire loops (i,j) I will get the first matrix (20,100) then after that for L loop I will get the other matrices. I want Uft(1,1) to add to the Uft(1,1) from old matrix until end of loop L (this mean for ten times)
this scenario for for new A(1,2)=old A(1,2) and so on
Andrei Bobrov
Andrei Bobrov 2015년 10월 24일
What is it: Xc, Yc, Y, VCo, U.
Ali Kareem
Ali Kareem 2015년 10월 24일
편집: Ali Kareem 2015년 10월 24일
Hi, Thank you for your reply. Xc, Yc, Y, VCo, U. all these are calculated in another part of program.
Regards
Andrei Bobrov
Andrei Bobrov 2015년 10월 24일
Xc, Yc, Y, VCo, U - arrays or constants?
Ali Kareem
Ali Kareem 2015년 10월 24일
편집: Ali Kareem 2015년 10월 24일
Hi,
Thank you for your reply. U,VCo constant. Xc,Yc,Y arrays.
Regards
Andrei Bobrov
Andrei Bobrov 2015년 10월 25일
편집: Andrei Bobrov 2015년 10월 25일
Hi Ali! What is the size of your arrays (Xc,Yc,Y)?
:)

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

질문:

2015년 10월 23일

편집:

2015년 10월 25일

Community Treasure Hunt

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

Start Hunting!

Translated by