Compare each element of matrix

I have Matrix A and Matrix B with same dimension size, Matrix C = A-B,I want ot show an error if each element in C<=0, if C>0 execute other commands

답변 (1개)

Birdman
Birdman 2018년 4월 5일

0 개 추천

if all(C<=0)
disp('Error');
elseif all(C>0)
%code goes here
end

댓글 수: 2

siyang hua
siyang hua 2018년 4월 5일
A = [2 3 4]; B = [1 5 4]; C = A-B; if all(C<=0) disp('Error'); elseif all(C>0) disp('good') end I try this, but the command window only shows the Mactrx C without any 'error' massage
Because the resultant C matrix is
C=[1 -2 0]
which neither fits for all(C<=0) nor all(C>0). They both give logical output 0. You may want to create a third statement like:
A = [2 3 4];
B = [1 5 4];
C = A-B;
if all(C<=0)
disp('Error');
elseif all(C>0)
disp('good');
else
disp('none');
end

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

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

질문:

2018년 4월 5일

댓글:

2018년 4월 6일

Community Treasure Hunt

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

Start Hunting!

Translated by