Compare current iteration with previous one?

7 visualizaciones (últimos 30 días)
Drazen Tubic
Drazen Tubic el 22 de Ag. de 2015
Comentada: Drazen Tubic el 23 de Ag. de 2015
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?

Respuesta aceptada

Walter Roberson
Walter Roberson el 22 de Ag. de 2015
Have you considered using min() ?

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by