Using logical operator in an if condition

Hi, I want my 'if' condition as follows:
if true
if (x==1) && (A(2,1)==1 || A(2,2)==1 || A(2,3)==1)
...
end
(Please disregard the first line with 'if true' )
I mean, I want my condition to be x equals 1 and one of A(2,1) A(2,2) or A(2,3) to be equal 1 as well. I think, the way that the expression above is written, is incorrect because even the condition isn't satisfied, program continues to execute the if statement.
How should I write the above condition?
Thank you Kaan Yilmaz

답변 (2개)

Roger Stafford
Roger Stafford 2016년 6월 5일
편집: Roger Stafford 2016년 6월 5일

0 개 추천

You are using the "short circuit" logical operators. They work for scalars but not for vectors with more than one element. In the latter case change to '&' and '|'.
Another possibility for your difficult is that you are requiring exact equality which may not be satisfied if x, y, z, or t differ from 1 in their least binary digits from rounding errors.

댓글 수: 2

Kaan Yilmaz
Kaan Yilmaz 2016년 6월 5일
편집: Kaan Yilmaz 2016년 6월 5일
I wrote y z t for simplicity but they are actually elements of a matrix and they are like A(2,1) A(2,2) and A(2,3). Their values are not computed but assigned by a function so a rounding error can not be a consideration.
What is unexpected here is that even all of the elements of the A matrix are 0, the code pretends like the condition is satisfied which requires at least some elements to be 1.
I also tried to use & and | but it was not a solution at all.
Thank you,
Roger Stafford
Roger Stafford 2016년 6월 5일
편집: Roger Stafford 2016년 6월 5일
I would like to see your actual code that you are using to produce the wrong result, including how A is generated.

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

aakash jain
aakash jain 2021년 3월 4일

0 개 추천

Error in untitled1233 (line 4)
if Z == 0 || X==P
ERROR

카테고리

도움말 센터File Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

질문:

2016년 6월 5일

답변:

2021년 3월 4일

Community Treasure Hunt

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

Start Hunting!

Translated by