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?