MATLAB Answers

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

조회 수: 2(최근 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
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개)



Community Treasure Hunt

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

Start Hunting!

Translated by