Effacer les filtres
Effacer les filtres

MatLab Logical answer always 1

4 vues (au cours des 30 derniers jours)
Matlab_Student
Matlab_Student le 26 Sep 2017
Commenté : Jan le 26 Sep 2017
I have 2017b installed on my mac. Below is the confusing part:.....
>> islogical(5<7)
ans =
logical
1
>> islogical(5>7)
ans =
logical
1
  1 commentaire
Stephen23
Stephen23 le 26 Sep 2017
Modifié(e) : Stephen23 le 26 Sep 2017
The MATLAB documentation for islogical states clearly that " tf = islogical(A) returns true if A is a logical array and false otherwise" (emphasis added). It does not state anywhere on the page that islogical tests what the value of the logical array is.
Lets try it. Both true and false are scalar logical arrays, so we would expect them both to return true if we test if they are logical arrays:
>> islogical(true)
ans = 1
>> islogical(false)
ans = 1
>>
So no surprises there: a logical array is a logical array.

Connectez-vous pour commenter.

Réponse acceptée

Cedric
Cedric le 26 Sep 2017
Modifié(e) : Cedric le 26 Sep 2017
Yes, all these tests (relational operators) return a logical, which is a value that is true (1) or false (0).
But:
>> 5 < 7 % Returns true (symbolized by 1).
ans =
logical
1
>> 5 > 7 % Returns false (symbolized by 0).
ans =
logical
0
  4 commentaires
Cedric
Cedric le 26 Sep 2017
Modifié(e) : Cedric le 26 Sep 2017
As Walter is pointing out, ISLOGICAL is a test of class/type, like ISCHAR for characters. And the class of what is "returned" by a test invovling logical operator is "logical".
>> class(5<7)
ans =
logical
Of course 5<7 has a value (here true), but this value has a class like all other types of data in MATLAB.
So ISLOGICAL is not "isTrue" (which doesn't exist by the way .. it would return true when true and false otherwise ;)).
Jan
Jan le 26 Sep 2017
In other words: islogical(x) is equivalent to:
strcmp(class(x), 'logical')
or
isa(x, 'logical')
Remark: isa() is surprisinlgy slow: it needs about 4 times longer than the strcmp method.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Characters and Strings dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by