logical vector equality
조회 수: 8 (최근 30일)
이전 댓글 표시
Often I run into a situation where I need to determine if two vectors are equal and do something if they are. However, vec1==vec2 gives me a vector - not a logical 0 or 1. So I have to mess with that and it seems that Matlab should be able to tell me if two vectors or even structures are equal. However, I am not smart enough to figure how to make it do that without writing extra code every time it arises.
My question is how to I get a 0 or 1 result from v1==v2 when they are vectors?
댓글 수: 0
채택된 답변
Paulo Silva
2011년 7월 2일
all(vec1==vec2)
and if you want to ignore the difference between column and row vectors do
all(vec1(:)==vec2(:))
There's also this way to compare
isequal(vec1,vec2)
With the isequal you don't have the problem with the dimensions mismatch like you have with the case vec1==vec2
댓글 수: 1
Matt Fig
2011년 7월 2일
In addition to what Paulo says, IF statements automatically use ALL for vector conditionals. For example:
A = [1 2 3 4];
B = A;
if A==B
disp('InIF')
end
But now:
B(4) = 7;
if A==B
disp('InIF')
end
So if you know A and B will always have the same size and type, using == with an IF statement is fine. However, if there is any doubt about size or type, ISEQUAL is preferred.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!