How to get rid of Warning"Unexpected use of '[' in a scalar context."

17 Ansichten (letzte 30 Tage)
a part of the function has:
if the input A (a vector) matches what is stated, the statement runs.
if A == [1,3,3]
% statement
end
and it gives me this error: 'Unexpected use of '[' in a scalar context. How can I fix this?
  2 Kommentare
dpb
dpb am 13 Aug. 2018
Must be more than that; that code runs w/o any error here...
>> A=3;
>> if A==[1, 3, 3],disp('ok'),end
>> A=[1 3 3];
>> if A==[1, 3, 3],disp('ok'),end
ok
>>
Show us the context including the error message with all the text generated...
Crystal Lee
Crystal Lee am 13 Aug. 2018
There is no red text, I was just wondering if I could get rid of the orange warning.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Stephen23
Stephen23 am 13 Aug. 2018
Bearbeitet: Stephen23 am 13 Aug. 2018
Use all or any, like this:
if all(A==[1,3,3])
Or right-click the pop-up warning box and follow the instructions to suppress it.
  3 Kommentare
Thiago  Rodrigues
Thiago Rodrigues am 17 Sep. 2020
What the explanation for the "all" fix the warning?
Stephen23
Stephen23 am 17 Sep. 2020
Bearbeitet: Stephen23 am 17 Sep. 2020
"What the explanation for the "all" fix the warning?"
Given a vector input the output from all is scalar. A scalar condition is easier to understand, which is why it is recommended by that warning.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

KSSV
KSSV am 13 Aug. 2018
When you use A == [1 3 3] ;
With this operator ==, MATLAB expects a scalar value, but the way used here, it gives array as output. So the warning pops out. The way you use, won't work out. YOu need to use the operator == with a scalar value, so the if condition works fine.
  5 Kommentare
KSSV
KSSV am 13 Aug. 2018
Yes....it is not a error..it is a warning....I am using 2015b.
Crystal Lee
Crystal Lee am 13 Aug. 2018
Sorry, I confused warning with error.

Melden Sie sich an, um zu kommentieren.


David
David am 7 Dez. 2024
You may want to use isequal instead of == for that use case.

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Tags

Produkte

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by