Compare current iteration with previous one?
7 ビュー (過去 30 日間)
古いコメントを表示
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?
0 件のコメント
採用された回答
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Loops and Conditional Statements についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!