7

After I run the code in matlab, I encounter this error and unsure how to solve it. How can I solve this problem.

Warning:

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

    Jgray = double(rgb2gray(J));
    % Calculate the Gradients
    [dIx, dIy] = gradient(Jgray);
    if max(dIx)<=103 && max(dIy)<=100
        B =  abs(dIy) - abs(dIx);
    else
        B = abs(dIx) - abs(dIy);
    end
Community
  • 1
  • 1
Kim
  • 347
  • 2
  • 7
  • 18

2 Answers2

7

If dIx and dIy are matrices (as opposed to 1-D vectors), max(dIx) and max(dIy) will return vectors.

&& and || should be used to compare scalars, not vectors.

You probably want to type

if max(dIx(:))<=103 && max(dIy(:))<=100

but I cannot tell for sure, as I dont know what the code is supposed to do :)

Yamaneko
  • 3,433
  • 2
  • 38
  • 57
user1083059
  • 160
  • 1
  • 6
5

Use & and | for matrixes instead of &&, || .

&& and || are short circuit operators. If you think about it, they make no sense for matrixes. For example, the short circuit or - || stops and returns true whenever the first argument is true.
But how would you extend that to a matrix?

Andrey Rubshtein
  • 20,795
  • 11
  • 69
  • 104