Coding bug - IF statement

조회 수: 7 (최근 30일)
Anamil Mehta
Anamil Mehta 2020년 12월 7일
댓글: Ameer Hamza 2020년 12월 7일
I am working on a code right now where I need to compare a particular column of a matrix using IF statement. For some reason it wasn't working so I tried something simpler -
------
a=[1;0;0]
b=[0;0;1]
if a~=b
disp("hfsdfsdkhl")
end
-----
I am relatively new on MATLAB and dont get why the IF statement doesnot satisfy. This would not work either. Can someone help, please.

채택된 답변

Ameer Hamza
Ameer Hamza 2020년 12월 7일
편집: Ameer Hamza 2020년 12월 7일
Comparing arrays like this does not work properly with the if-else block. See the output of your comparison
>> a=[1;0;0]
b=[0;0;1]
a =
1
0
0
b =
0
0
1
>> a~=b
ans =
3×1 logical array
1
0
1
It is a 3 element vector. You can use isequal() to see if all elements are equal or not
a=[1;0;0];
b=[0;0;1];
if ~isequal(a,b)
disp("hfsdfsdkhl")
end
You can also use
any(a~=b)
% or
all(a==b)
  댓글 수: 2
Anamil Mehta
Anamil Mehta 2020년 12월 7일
That makes sense! Thank you!
Ameer Hamza
Ameer Hamza 2020년 12월 7일
I am glad to be of help!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

태그

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by