summation of valuse in nested loops
Ältere Kommentare anzeigen
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 Kommentare
Star Strider
am 23 Okt. 2015
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
am 23 Okt. 2015
Bearbeitet: Ali Kareem
am 24 Okt. 2015
Antworten (1)
Andrei Bobrov
am 24 Okt. 2015
Bearbeitet: Andrei Bobrov
am 24 Okt. 2015
z = 3;
ii = 10;
jj = 20;
k = 100;
z2 = z^2;
A = (z2+sin(1:jj)'*ones(1,k))*ii;
6 Kommentare
Ali Kareem
am 24 Okt. 2015
Andrei Bobrov
am 24 Okt. 2015
What is it: Xc, Yc, Y, VCo, U.
Ali Kareem
am 24 Okt. 2015
Bearbeitet: Ali Kareem
am 24 Okt. 2015
Andrei Bobrov
am 24 Okt. 2015
Xc, Yc, Y, VCo, U - arrays or constants?
Ali Kareem
am 24 Okt. 2015
Bearbeitet: Ali Kareem
am 24 Okt. 2015
Andrei Bobrov
am 25 Okt. 2015
Bearbeitet: Andrei Bobrov
am 25 Okt. 2015
Hi Ali! What is the size of your arrays (Xc,Yc,Y)?
:)
Kategorien
Mehr zu Matrix Indexing finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!