Is there something simpler than `~ isempty (x)` to distinguish a non-scalar `x` from a Boolean scalar?

short version

Is there a less cumbersome way (as commonly used in other languages) to "booleanize" non-scalar xthan ~isempty(x)?


tl; dr version

In many languages, such as Python, when variables / characters are evaluated in a Boolean context, they are automatically translated into a Boolean scalar. In particular, in such a context, a data structure like lists xis automatically converted to false if it is empty, otherwise true.

This means that you can write arbitrary Boolean expressions using lists as operands. For instance:

>>> list1 = [1, 1]
>>> list2 = [2, 2, 2]
>>> list3 = [3, 3, 3, 3]
>>> yesno = list1 and list2 and list3
>>> if yesno:
...   print True
... else:
...   print False
... 
True

In MATLAB, this does not quite work. for instance

>> list1 = [1 1];
>> list2 = [2 2 2];
>> list3 = [3 3 3 3];
>> yesno = list1 && list2 && list3;
Operands to the || and && operators must be convertible to logical scalar values. 
>> yesno = list1 & list2 & list3;
Error using  & 
Matrix dimensions must agree. 

, , - :

>> yesno = ~isempty(list1) && ~isempty(list2) && ~isempty(list3);
>> if yesno
  true
else
  false
end
ans =
     1

, ~isempty(...) "booleanizing" a   MATLAB?

+4
2

: , ~ isempty() - .

yesno = ~isempty(l1)*~isempty(l2)*~isempty(l3)

0

isempty .

, :

list1 = [1, 1];
list2 = [2, 2, 2];
list3 = [3, 3, 3, 3];
list4 = [];

yesno = all(~cellfun(@isempty, {list1,list2,list3,list4}))

, :

lists{1} = [1, 1];
lists{2} = [2, 2, 2];
lists{3} = [3, 3, 3, 3];
lists{4} = [];

yesno = all(~cellfun(@isempty,lists))

cellfun isempty , .

+2

All Articles