フィルターのクリア

Howto store the value in 'for loop' to be compared in the next iteration

2 ビュー (過去 30 日間)
Kelzang choden
Kelzang choden 2021 年 3 月 20 日
コメント済み: Jan 2021 年 3 月 24 日
How to store the value in 'for loop' to be compared in the next iteration for the same for loop. if the value obtained is lesser than the previous value, i have to replace it with the new one.
In the following code, the Ploss that we get in the first iteration have to compare with the Ploss we get in the second iteration. And if Ploss of second iteration is less than the first one, the value is stored. And continue till max ieration is reached. How to code for such problem??
for iter=1:10
for i=1:5
.
.%do something
.
Ploss(i)= objfun(i);
end
end

採用された回答

Jan
Jan 2021 年 3 月 20 日
編集済み: Jan 2021 年 3 月 20 日
Exactly as you have written it as text: "if the value obtained is less than the previous value, i have to replace it with the new one":
Ploss = inf(1, 5);
for iter = 1:10
for i = 1:5
...
% Compact solution:
Ploss(i) = min(Ploss(i), objfun(i));
% Or more literally:
c = objfun(i);
if c < Ploss(i)
Ploss(i) = c:
end
end
end
  2 件のコメント
Kelzang choden
Kelzang choden 2021 年 3 月 24 日
@Jan whai if the Condition is changed as given: Here if the ploss(i2) obtained is less than previous value i want to replace it.
for iter=1:10
for i=1:5
%do something
ploss(i)=losses(i)
end
for i1=1:2
%do something
ploss(i1)=losses(i1)
end
for i2=1
% do something
ploss(i2)=losses(i2)
end
end
Jan
Jan 2021 年 3 月 24 日
I'm not sure, if I understand you correctly, but this should work in exactly the same way:
ploss(i2) = min(ploss(i), losses(i2));
or
if anything < anotherThing
value = anything;
end

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by