What does a = b > c specify in MATLAB syntax?

조회 수: 5 (최근 30일)
Sarim Khan
Sarim Khan 2017년 10월 12일
댓글: Walter Roberson 2022년 11월 15일
My guess is that it is a shorthand for an if-else condition where a = 1 if b>c is true. Is this correct? If yes, can I replace a = b > c with a suitable if else condition?

채택된 답변

Walter Roberson
Walter Roberson 2017년 10월 12일
Yes, you can replace it with
if b > c
a = logical(1);
else
a = logical(0);
end
Another way of expressing this is:
if b > c
a = true;
else
a = false;
end
This is not the same as
if b > c
a = 1;
else
a = 0;
end
because in this later code, a = 1 or a = 0 assigns values of class double() to a, which behaves differently than when values of class logical() are assigned to a .
  댓글 수: 1
Walter Roberson
Walter Roberson 2022년 11월 15일
Note that using if/else like this is only valid for the case that a and b are both scalars.

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

추가 답변 (1개)

Matt J
Matt J 2017년 10월 12일
편집: Matt J 2017년 10월 12일
No, it is not a shorthand for if/else.
b>c is an expression that returns a value of true() or false(). In this case, the returned value is simply assigned to a.

카테고리

Help CenterFile Exchange에서 Argument Definitions에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by