필터 지우기
필터 지우기

how to replace "&&" in a loop

조회 수: 1 (최근 30일)
Alan Robertson
Alan Robertson 2020년 3월 6일
편집: Adam Danz 2020년 3월 6일
I would like not to use "&&" in this loop:
if a=x && b=y
d=2
end
how can i replace it?

답변 (1개)

Adam Danz
Adam Danz 2020년 3월 6일
편집: Adam Danz 2020년 3월 6일
"how to replace "&&" in a loop"
If, for whatever reason, you don't like the && symbols,
if a==x && b==y
d=2;
end
could be rewritten as
if all([a==x,b==y])
or
if all([isequal(a,x), isequal(b,y)])
or perhaps you'd like to use indexing as Stephen pointed out,
idx = a==x & b==y;
d(idx) = 2;
  댓글 수: 2
Stephen23
Stephen23 2020년 3월 6일
편집: Stephen23 2020년 3월 6일
If any of a, b, x, or y are non-scalar then your proposal will give a very different response to the original code.
I suspect that the OP actually has non-scalar data, and that indexing would be a much more suitable solution.
Adam Danz
Adam Danz 2020년 3월 6일
The original code would throw an error at if a=x but I'll up date my answer to cover the indexing interpretation.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by