MATLAB: Compare current iteration with previous one

for loopif statementMATLABmatrixmatrix arraystruct

How can I compare the current iteration in a loop with the previous one?
For example, I want to see if an element in the matrix, which is part of a structure, is lower than that same element, but from the previous iteration (obviously these aren't the same values, but the same elements, just different iterations).
a=rand(20,1);
field = 'field';
for h=1:20
value = 1.4*rand(20,1);
value1 = zeros(20,1);
mystruct(h) = struct(field,value);
NEWstruct(h) = struct(field,value1);
end
for j=1:20
if mystruct(1).field(j,1)<a(j,1)
NEWstruct(1).field(j,1)=mystruct(1).field(j,1);
else
NEWstruct(1).field(j,1)=a(j,1);
end
end
Now after this I have to see if next iteration `mystruct(2).field(j,1)` is lower than previous iteration `NEWstruct(1).field(j,1)`, and if it is, assign it's value to `NEWstruct(2).field(j,1)`. If it's not, then it should be equal to `mystruct(1).field(j,1)`, and so on for the rest of the 20 iterations. Is it possible to do that with if statement or should it be done with other method?

Best Answer

Have you considered using min() ?