octave-maintainers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Matlab check for negation of empty matrix


From: John W. Eaton
Subject: Re: Matlab check for negation of empty matrix
Date: Thu, 27 Mar 2014 16:18:07 -0400
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20131005 Icedove/17.0.9

On 03/27/2014 12:00 PM, Rik wrote:
3/27/14

Could someone run the following code under Matlab?

x = [];
if (~x)
   disp ('branch 1');
else
   disp ('branch 2');
end

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

  if (X)

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
the argument.

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;
      break;
    end
  end

  any_nonzero = 0;
  for i = 1:numel (x)
    if (x(i) != 0)
      any_nonzero = 1;
      break;
    end
  end

But in any case, we don't get to choose this behavior if we want to
remain compatible with Matlab.

jwe



reply via email to

[Prev in Thread] Current Thread [Next in Thread]