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

156 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.

## Related questions

1 vote
23 views
Problem: I am new in programming so I tried python practice I tried to figure out f any operator represents the logical and. in python programming can anybody help?
1 vote
40 views
Problem: For the following code : E = [5,200]; Selected edge values X = imread('LENNA128.bmp'); N = length(X); Y = false(N+2); for k = 1:numel(E); Y(2:end-1,2:end-1) = X==E(k); Z = Y(1:end-2,2:end-1) | Y(3:end,2:end-1) | Y(2:end-1,3:end) ... )/2); end I get the error message: In an assignment A(:) = B, the number of elements in A and B must be the same Can anyone help me out to solve this problem.