Call a certain function at the last iteration if flag is set to true

1 回表示 (過去 30 日間)
hmhuang
hmhuang 2021 年 11 月 26 日
回答済み: Walter Roberson 2021 年 11 月 26 日
I have a logic as following:
plot_final_iteration_only = true;
for iter = 1 : max_iter
% Do stuff...
if (plot_final_iteration_only)
if (iter == max_iter)
plotIteration(...);
end
else
plotIteration(...);
end
end
Is there any way to avoid writing plotIteration(...); for two times (which is a bit redundant for me), but with the same logic?

採用された回答

Walter Roberson
Walter Roberson 2021 年 11 月 26 日
plot_final_iteration_only = true;
for iter = 1 : max_iter
% Do stuff...
if ~plot_final_iteration_only || iter == max_iter
plotIteration(...);
end
end

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by