understanding an if statement

조회 수: 3 (최근 30일)
kalana agampodi
kalana agampodi 2021년 10월 28일
편집: DGM 2021년 10월 28일
What does this mean ?
Can you please explain with an example ?
if not(max(c))c
Thanks

채택된 답변

DGM
DGM 2021년 10월 28일
편집: DGM 2021년 10월 28일
This isn't very clear usage and looks like invalid syntax at first glance. It would help to know what the surrounding code looks like and what c is (its size and type).
What looks like invalid syntax (an implicit multiplication with the trailing instance of c) is actually parsed like this:
c = [0 1 2 3]; % let's assume c is a numeric or logical vector
if not(max(c)) % true if largest value of c is <= 0
c % this is treated as a separate expression on another line
disp('tested true')
else
disp('tested false')
end
tested false
The condition will only test true if all elements of c are less than or equal to zero. You could do the same thing like so:
if all(c<=0) % true if c contains no positive nonzero values
% ...
end
Given the way this is written, it's not clear that this is the actual intent.
If the intent is for the test to be true only when all elements of c are equal to zero, you could do that instead:
if all(c==0) % true if c contains no nonzero elements
% ...
end
or
if nnz(c) == 0 % true if c contains no nonzero elements
% ...
end
or
if ~any(c) % true if c contains no nonzero elements
% ...
end

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Weather and Atmospheric Science에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by