Basic comparison between columns
조회 수: 2 (최근 30일)
이전 댓글 표시
Hello all,
Let's say I have a 10 x 10 matrix filled with numbers (10 x 10 double). When the value 999 is found (anywhere) the value in the row on the right-hand side must be 9 or it's an error. I would have thought that:
if Z(:,1:end) == 999 & Z(:,2:end)~=9
disp('error)
return
end
would work but it doesn't.
Any ideas? Thank you
댓글 수: 1
the cyclist
2017년 4월 12일
편집: the cyclist
2017년 4월 12일
When you say "right-hand side", do you mean the element just the right of each occurrence of 999 needs to be equal to 9, or that 9 needs to be in the right-most column of the matrix?
How should it be handled if 999 is in the right-most column?
채택된 답변
the cyclist
2017년 4월 12일
편집: the cyclist
2017년 4월 12일
Assuming 9 needs to be just to the right of 999 (and not handling the last column at all), I would probably do this as follows:
conditionFail = Z(:,1:end-1)==999 & not(Z(:,2:end)==9)
if any(conditionFail(:))
disp('error)
return
end
It is possible to put all the code into if statement (without needing to pre-specify the condition), like this ...
if any(any(Z(:,1:end-1)==999 & not(Z(:,2:end)==9)))
disp('error)
return
end
but I think that is more obfuscated.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!