Could someone run the following code under Matlab?
x = ;
disp ('branch 1');
disp ('branch 2');
There have been two issues with this construct in the last two days.
Coming from a scripting background like Perl or Python, I would expect 
to evaluate as false and '~' to negate this to true. But, it seems that
~ is still  which means isempty should really always be used instead of
the negation operator.
> Could someone run the following code under Matlab?
> x = ;
> if (~x)
> disp ('branch 1');
> disp ('branch 2');
> There have been two issues with this construct in the last two days.
I think this is one of the really ugly parts of Matlab.
Normally, you can think of
as being "if all the elements of X are nonzero, the condition is
true. But the expression "all ()" is true, but "if ()" evaluates
the condition as false, presumably because someone decided that it was
too confusing for  to be considered "true", or maybe it was just an
accident of the original implementation that "all ()" is true. I
mean, there are no elements, so saying that they are all zero
seems just as valid to me as saying that they are all nonzero (or any
other value, or no value at all).
> Coming from a scripting background like Perl or Python, I would expect 
> to evaluate as false and '~' to negate this to true. But, it seems that
> ~ is still  which means isempty should really always be used instead of
> the negation operator.
Except the IF statement doesn't examine the expression, it just
evaluates it. So it only sees the result of ~, which is  since
there are no elements to negate. I guess what you are looking for is
some kind of "matrix negation" operator which would evaluate its
operand to a single logical value (presumably false in this case) and
then negate that. But I don't think that operator exists in Matlab or
Octave. If that's what you want, I think you have to write some kind
of expression like "all (X(:))"
Even stranger, at least to me:
all () => 1
any () => 0
So, uh, let me get this straight, all the elements of  are nonzero,
but there aren't any that are nonzero? Are these really the correct
results, or am I just not smart enough to understand this part of the
algebra of empty matrices? Shouldn't these results just be (1x0)
since there are no columns and these functions operate over the
columns of their arguments? In other cases in which the argument is a
matrix (not a vector, the special cases for which are another source
of ugliness), I believe the result has the same number of columns as
Note that these results are consistent with an implementation similar
to the following (which is why I wonder if these results are simply
accidental, but kept as they are for backward compatibility):
all_nonzero = 1;
for i = 1:numel (x)
if (x(i) == 0)
all_nonzero = 0;
any_nonzero = 0;
for i = 1:numel (x)
if (x(i) != 0)
any_nonzero = 1;
But in any case, we don't get to choose this behavior if we want to
remain compatible with Matlab.
On Thu, Mar 27, 2014 at 04:18:07PM -0400, John W. Eaton wrote:
> all () => 1
> any () => 0
If  is interpreted as in the special case of a vector argument (no
column-wise operation) it makes sense to me: any statement regarding
the particular elements of  is true (since there are no elements) so
it is also true that "all" elements are true; but "any" seems to imply
that elements exist, so it is false.
> Shouldn't these results just be (1x0)
> since there are no columns and these functions operate over the
> columns of their arguments?
If  is interpreted as a matrix argument, yes. I agree that treating
a vector argument as a special case is a source of inconsistency.
> But in any case, we don't get to choose this behavior if we want to
> remain compatible with Matlab.
public key id EAFE0591, e.g. on x-hkp://pool.sks-keyservers.net