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

365 views (last 30 days)
Nikhil
Nikhil on 31 May 2013
Commented: Brian Fournet on 28 Feb 2021
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 Comments
Arthur Joker
Arthur Joker on 28 Nov 2018
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!

Sign in to comment.

Accepted Answer

James Tursa
James Tursa on 31 May 2013
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.

More Answers (2)

Walter Roberson
Walter Roberson on 31 May 2013
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 Comments

Sign in to comment.


Nikhil
Nikhil on 31 May 2013
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 Comment
Iain
Iain on 31 May 2013
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.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!