For each loop upon fixed size array

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 .

 Risposta accettata

for k=1:100
A=A+1;
end

4 Commenti

Jimmy cho
Jimmy cho il 16 Nov 2020
Modificato: Jimmy cho il 16 Nov 2020
thanks, but lets assume that I want to compare between each two values of the array by for each loop? I mean
for each loop upon array A values
if (A > A++ ) % this statement is about comparing A(i) > A(i+1) but I want by for each loop and not by indexes ..
%do something
how can I do that in matlab by for each loop to Pass/Run over the values of the array? I mean for each loop ..not by indexes (A(i))
Image Analyst
Image Analyst il 16 Nov 2020
Modificato: Image Analyst il 16 Nov 2020
Why by a for loop instead of by using the diff() or conv2() function?
Can't I do that in matlab? for instance in python ..and the reason is that foreach it's more elegant that using idexes.
another question in the context, lets assume I have array A=[x y z ] and x , y , z as I said in my thread above is integer values, how I pass/run over A array by foreach in matlab code?
just like this:
for m:A
%do something with m / do arithmatic with m , m is the value of array A and next value of array A updated to m at each iteration
end
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.

Accedi per commentare.

Più risposte (1)

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 Commenti

" Acopy = A + k " in this statement the A is changed at every iteration to the next value organizedly ? I mean first value at 1, then next iteration value at 2 , then next iteration value at 3 ?
If I understand you well, if I do this:
for k = 1 : 100
m = A ;
end
so every value of A will be copied / stored to m correspondingly , yes?
Implicitly A is going to the next value at every iteration

Accedi per commentare.

Categorie

Community Treasure Hunt

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

Start Hunting!

Translated by