Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

matlab - How to solve && operands to logical scalar

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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 :)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...