For each loop upon fixed size array
33 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Jimmy cho
am 16 Nov. 2020
Kommentiert: Jimmy cho
am 16 Nov. 2020
Hi guys,
I have three variable x y z that are integer values and for instance its values are :
x=5 , y=2 , z=3;
and I have an array called A equal to A=[x y z]
I want to do for each loop upon the array A and adding +1 for every iteration of the for each loop .
I came from world of c world and rarely I use matlab (it depends on what do I want to implement ) .. how can I do that in matlab?
thanks alot for any help .
0 Kommentare
Akzeptierte Antwort
David Hill
am 16 Nov. 2020
for k=1:100
A=A+1;
end
4 Kommentare
Image Analyst
am 16 Nov. 2020
Well most MATLABers would think
result = diff(A) > 0;
is more elegant than using for or foreach. The diff() function subtracts the kth element from the (k+1)st element.
result is a logical matrix giving essentially a "map" of what elements are greater than the prior element.
Weitere Antworten (1)
Image Analyst
am 16 Nov. 2020
Another option
Acopy = A; % Make copy of original
for k = 1 : 100
Acopy = A + k;
end
The original A does not get changed, however Acopy still gets overwritten at each iteration.
2 Kommentare
Siehe auch
Kategorien
Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!