Hello,
I do not know what means this message error:
Operands to the logical AND (&&) and OR (||) operators must be convertible to logical scalar values. Use the ANY or ALL
functions to reduce operands to logical scalar values.
Error in EXAMPLE_GA_TOUR1 (line 35)
while cond<500 && gg<100
Can anybody help

답변 (1개)

Walter Roberson
Walter Roberson 2022년 8월 17일

0 개 추천

The && operator is the "short circuit and" operator. A && B evaluates A, and if A is non-zero, then it evaluates B -- but if A is zero then it does not evaluate B because it knows the test will fail.
The && operator is restricted to working on scalar values.
Looking at the error message, we can predict that either cond is not a scalar, or else gg is not a scalar. You need to think more about what you want to have happen. Do you want to exit the while loop as soon as even one cond value is >= 500? Or do you want to continue the while loop until all cond values are >= 500 ?
You should be using any() or all() to reflect what you want to happen when the variables are non-scalar. Or possibly you should be reviewing your logic to figure out how cond or gg accidentally became non-scalar.

댓글 수: 5

huda nawaf
huda nawaf 2022년 8월 17일
I need even one cond value is >=500
Walter Roberson
Walter Roberson 2022년 8월 18일
편집: Walter Roberson 2022년 8월 18일
while all(cond<500) && gg<100
will stop the loop as soon as even one cond value is >= 500
while any(cond<500) && gg<100
will not stop the loop until all of the cond values are >= 500
huda nawaf
huda nawaf 2022년 8월 18일
But it stopped when one cond met the condition which is 500, what happened was that a message error appeared when making the variable cond have an index:
cond(gg)=max(a)
I do not know if that is related to that message. But the message no longer appears when removing the index gg
Walter Roberson
Walter Roberson 2022년 8월 18일
Sorry, that is too vague for us to guess what your code is doing.
huda nawaf
huda nawaf 2022년 8월 24일
I want to clarify that
while cond<500 && gg<50
It is working correctly once one of the two conditions is met, the processing will stop. In other words, while the two condition are not met together, the code continue
Not necessary using all.
In fact the cond variable give me different values each time , I wanted to save the values. So saved it in cond(gg) where gg is variable increase one each time.
while cond(gg)<500 && gg<50
I think the message error that appeared becuase of cond(gg), once remove it it is no longer appear.
I hope that clarify the problem.
Thanks

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

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

제품

릴리스

R2022a

질문:

2022년 8월 17일

댓글:

2022년 8월 24일

Community Treasure Hunt

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

Start Hunting!

Translated by