Efficient way to evaluate multiple logic expression as in 'if' condition

1 回表示 (過去 30 日間)
Hainan Wang
Hainan Wang 2021 年 5 月 1 日
コメント済み: Hainan Wang 2021 年 5 月 1 日
I am running an iterative algorithm, which will generate a row vector with 10 parameters. After one iteration, the new row vector, or 10 new parameters, will augment to the previous row vector to form a matrix ‘u’. The element in the same column shows the variance/change of the same variable. For all variables, when the absolute deviation of current value versus the value from previous iteration is less than a tolerance, 1e-7, I want algorithm terminates. Then the termination condition can be expressed as,
abs(u(i+1,k)- u(i,k))<= 1e-7, for k = 1,…,10
That could be easily achieved with following code:
if abs(u(i+1,1)- u(i,1))<= 1e-7&& ...
abs(u(i+1,2)- u(i,2))<= 1e-7&& ...
abs(u(i+1,3)- u(i,3))<= 1e-7&& ...
abs(u(i+1,4)- u(i,4))<= 1e-7&& ...
abs(u(i+1,5)- u(i,5))<= 1e-7&& ...
abs(u(i+1,6)- u(i,6))<= 1e-7&& ...
abs(u(i+1,7)- u(i,7))<= 1e-7&& ...
abs(u(i+1,8)- u(i,8))<= 1e-7&& ...
abs(u(i+1,9)- u(i,9))<= 1e-7&& ...
abs(u(i+1,10)- u(i,10))<= 1e-7
break
end
My problem is now the number of parameters increases to 100, all other assumptions remain the same. I do not want to code the termination condition (though just copy/paste) by hand with 100 lines. Is there a easy way I can do this?

採用された回答

Yifeng Tang
Yifeng Tang 2021 年 5 月 1 日
something like
max(abs(u(i+1,:)-u(i,:))) <= 1e-7
do you think this will work?
  1 件のコメント
Hainan Wang
Hainan Wang 2021 年 5 月 1 日
Yes, it works! Thank you very much, that's neat and smart!

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

その他の回答 (0 件)

カテゴリ

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

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by