1
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?

Cris Luengo
  • 55,762
  • 10
  • 62
  • 120

1 Answers1

1

Assuming the ext_rows is a numerical array, you want to use & and | instead of && and ||.

The latter only work with with single (scalar) logical values, and their purpose is to skip unecessary logical checks in your code. See the help page for short circuits for more information. If your array is very large, consider using anyExceed.

To see why your example needs the first set of operators, create a variable to indicate if ext_rows is greater than 150. We then check if it is both logical and a scalar.

# note: I would consider rewriting it as ext_rows > 150 for readability
over150 = 150 < ext_rows; 
islogical(over150) # true
isscalar(over150) # false

Notice that the second call is false because over150 is an array of logicals that indicate if each element is greater than 150. The same will be true if ext_rows is less than 200.

under200 = ext_rows < 200;
islogical(under200) # passes
isscalar(under200) # fails

Using the above, which is essentially what you had originally, but now with an & to get the indices for all elements within the range, gives us the following:

idxWhitePixels = over150 & under200;
whitePixels = ext_rows(idxWhitePixels);