Compare current iteration with previous one?

7 vues (au cours des 30 derniers jours)
Drazen Tubic
Drazen Tubic le 22 Août 2015
Commenté : Drazen Tubic le 23 Août 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?

Réponse acceptée

Walter Roberson
Walter Roberson le 22 Août 2015
Have you considered using min() ?
  1 commentaire
Drazen Tubic
Drazen Tubic le 23 Août 2015
min() does the trick. Thank you

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by