필터 지우기
필터 지우기

How to solve this error "Operands to the || and && operators must be convertible to logical scalar values." ?

조회 수: 66 (최근 30일)
Hello
I am doing project on object recognition in which during classification i got this error "Operands to the and && operators must be convertible to logical scalar values." how to solve this? please help me to get out of this!
  댓글 수: 3
Nikhil
Nikhil 2013년 5월 31일
편집: Walter Roberson 2013년 5월 31일
Here is the code
if hyp1_min(1)< hyp2_min(1) < hyp2_max(1) < hyp1_max(1) && match == 'FALSE'
m= 1;
match = 'TRUE';
while 1;
Here i am checking condition, here hyp1_min, hyp2_min, hyp1_max, hyp2_max are matrix and in which there are total 17 values. so i am checking for each and every matrix values!! if all the values are true then only i ll do further procedure.
Thank you!
Arthur Joker
Arthur Joker 2018년 11월 28일
Hello, I'm using another software called Prescan. matlab 2017b will be started invoked by the prescan automatically. and then I got the error "Operands to the || and && operators must be convertible to logical scalar values". anyway, I can't modify the software of Prescan~
how to fix it if I can modify some configuration from Matlab?
Thanks!

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

채택된 답변

James Tursa
James Tursa 2013년 5월 31일
You get this when one or both operands are arrays. E.g.,
>> [1 2 3] && [4 5 6]
??? Operands to the || and && operators must be convertible to logical scalar values.
Check the two expressions or variables you are using for the or && operation. You are using them as scalars but they are not.

추가 답변 (2개)

Walter Roberson
Walter Roberson 2013년 5월 31일
If the problem is occurring in your code, change the && or || to & or | respectively. That would get rid of the error message, but probably not the logic error.
If the problem is occurring in one of MATLAB's NN routines, then you have passed the wrong shape of data for some argument to the call.
  댓글 수: 2
Walter Roberson
Walter Roberson 2013년 5월 31일
You have multiple problems:
1)
a < b < c
in MATLAB means
((a < b) < c)
The first part returns false (0) or true (1), so this expression becomes one of
0 < c
or
1 < c
which has a logical result.
To test if b is in the range a to c (exclusive), test
if a < b & b < c
2) Do not use "==" to compare strings. A string is a vector of characters, so you will get a vector result reflecting "is the first character F, true or false? Is the second character A, true or false?" The "==" would fail if the two are not the same length, just the same way that
[1 2] == [1 2 3]
is going to fail because of the different lengths.
Use strcmp() to compare strings.
3)
You need an "end" statement to terminate your "if" statement.
4)
The statement
while l;
needs additional code after it, terminating with an "end" statement. The code between the "while" and the "end" would be what is repeated. You can expression the action of doing nothing in the loop as
while l; end
What this would do is to test the variable "l", and if it consists entirely of non-zero values, then the loop body (with nothing in it) would be executed once. Then the loop would repeat and "l" would be tested again; if it was still all non-zero then the loop would repeat. Since nothing in the empty loop body changes "l" it is going to repeat infinitely if it repeats at all.
I recommend you read the introductory documents on MATLAB control statement syntax.

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


Nikhil
Nikhil 2013년 5월 31일
Here is the code
if hyp1_min(1)< hyp2_min(1) < hyp2_max(1) < hyp1_max(1) && match == 'FALSE' m= 1; match = 'TRUE'; while 1;
Here i am checking condition, here hyp1_min, hyp2_min, hyp1_max, hyp2_max are matrix and in which there are total 17 values. so i am checking for each and every matrix values!! if all the values are true then only i ll do further procedure.
Thank you!
  댓글 수: 1
Iain
Iain 2013년 5월 31일
Your logic is definitely wrong.
hyp1_min(1)< hyp2_min(1) < hyp2_max(1) < hyp1_max(1) is evaluated as ((hyp1_min(1)< hyp2_min(1)) < hyp2_max(1)) < hyp1_max(1) (((0 or 1) < some_value) < some_value ((0 or 1) < some value)
match == 'FALSE', will return a 5-element result, however, if match == 'TRUE', it will throw an error. Use "strcmp" to do string comparisons.

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

카테고리

Help CenterFile Exchange에서 Sequence and Numeric Feature Data Workflows에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by