On multiple logical test failing in if statement
4 ビュー (過去 30 日間)
古いコメントを表示
Hello, I am trying to run an if statement with multiple conditions as below,
if abs(a-a_t)>0.0001 && abs(b-b_t)>0.00001 && abs(c-c_t)>0.0001 && abs(d-d_t)>0.00001
x=0;
y=0;
end
Even when all the conditions are satisfied, the values of x and y are not updated. But, it works if we have just two conditions (I've tried different combinations of two variable values which work, but using all conditions in separate brackets doesn't work). MATLAB documentation says that multiple logical operators could be used, but it doesn't seem to be working in my case. Is there a limit to the number of logical operators that could be combined? Or, am I missing something?
Thanks for your advice.
2 件のコメント
Stephen23
2018 年 7 月 8 日
編集済み: Stephen23
2018 年 7 月 8 日
"Is there a limit to the number of logical operators that could be combined?"
No.
"Or, am I missing something?"
Probably. How are you checking the output? Please show us the exact input values (all eight of them). Please upload your code by clicking the paperclip button.
採用された回答
dpb
2018 年 7 月 8 日
編集済み: dpb
2018 年 7 月 8 日
Apparently the issue was likely one of the cases wasn't actually T or some similar misstep, but illustrates the difficulties inherent in writing code with sequentially-named variables. Use arrays instead.
If had defined a variable data and commensurate test value, then the following could be written much more succinctly and much easier to both code and debug--
data=[a b c ...]; % the data array (*)
test=[a_t b_t c_t ...]; % associated test values
lims=[0.0001 0.00001, 0.0001, ...]; % and criteria
if all(abs(data-test))<lims % see if all ok
....
(*) And, of course, this is not intended to suggest one should write the specific line concatenating all these variables to create the one; instead use the vector features of Matlab when creating the variables initially, either by reading one or more files or the result of other calculations, whatever.
Don't create the problem in the beginning to have to try to fix later...
7 件のコメント
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Matrix Indexing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!