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

5 vues (au cours des 30 derniers jours)
Sarim Khan
Sarim Khan le 12 Oct 2017
Commenté : Walter Roberson le 15 Nov 2022
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?

Réponse acceptée

Walter Roberson
Walter Roberson le 12 Oct 2017
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 commentaire
Walter Roberson
Walter Roberson le 15 Nov 2022
Note that using if/else like this is only valid for the case that a and b are both scalars.

Connectez-vous pour commenter.

Plus de réponses (1)

Matt J
Matt J le 12 Oct 2017
Modifié(e) : Matt J le 12 Oct 2017
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.

Catégories

En savoir plus sur Argument Definitions dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by