# Operands to the || and && operators must be convertible to logical scalar values.

446 views

## Problem :

I have very simple problem that currently looking for the fast implementation in Matlab. I have below array of values:

` b = floor(rand(5,5).*255)`

I also have the similarly sized threshold array :

`b_thresh = floor(rand(5,5).*255)`

I want to use the value of b unaltered. I thought I can use something like the below:

`b(b>= b_thresh .* 0.4 && b <b_thresh.* 0.5) = ((b - b_thresh.*0.4)/(b_thresh.*0.5 b_thresh.*0.4)) .* b;`

But, I am facing below error :

Operands to || and && operations must be convertible to logical scalar values

How can I resolve the issue?

## Solution :

Regarding your first problem : The thing about the && is that it can operate only on the scalars, whereas the & can operate on arrays as well. So you should change your && to & to make it work.

Now regarding your second problem : The number of the elements on your left is different because you are trying to use the indices and on your right you are trying to work with the entire matrix b and b_thresh.

So you need to use the indices in both sides, so I suggest you to store it in the variable and then use it as the array subscript, along below lines:

`myidx = (b >= b_thresh*0.4 & b < b_thresh*0.5);`
`b(myidx) = ((b(myidx)-b_thresh(myidx)*0.4) ./ (b_thresh(myidx)*0.5*b_thresh(myidx)*0.4)) .* b(myidx);`

I hope it solves your issue.

38.6k points

## Related questions

39 views
Problem: What could be the problem > Operands to the || and && operators must be convertible to logical scalar values. matlab
116 views
Problem: I am stuck with this ... Please help me that how to deal with this error? operands to the and && operators must be convertible to logical scalar values. matlab
16 views
Problem: whitePixels=ext_rows((150<ext_rows) && (ext_rows<200)); numberWhitePixels=numel(whitePixels); error : Operands to the || and && operators must be convertible to logical scalar values. How can I solve this error? Could you please write down the correct code here?
9 views
Problem: Operands to the and && operators must be convertible
1 vote
19 views
Problem: I am facing this problem on Mtlab please resolve it: Matlab operands to the and operators must be convertible.
183 views
Problem: Do you have any thoughts on how bitwise operators cannot be utilized on the float type? Thank you so much!
28 views
Problem: Why would a language NOT use Short-circuit evaluation? Are there any benefits of not using it? I see that it could lead to some performances issues... is that true? Why?