Compare current iteration with previous one?

조회 수: 7 (최근 30일)
Drazen Tubic
Drazen Tubic 2015년 8월 22일
댓글: Drazen Tubic 2015년 8월 23일
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?

채택된 답변

Walter Roberson
Walter Roberson 2015년 8월 22일
Have you considered using min() ?

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by