# Matlab check for negation of empty matrix

4 messages
Open this post in threaded view
|

## Matlab check for negation of empty matrix

 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. 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. --Rik
Open this post in threaded view
|

## Re: Matlab check for negation of empty matrix

 On 27/03/2014 16:00, Rik wrote: > 3/27/14 > > Could someone run the following code under Matlab? > > x = []; > if (~x) >    disp ('branch 1'); > else >    disp ('branch 2'); > end > r2012a  >> x = []; if (~x)    disp ('branch 1'); else    disp ('branch 2'); end branch 2 Richard -- The University of Edinburgh is a charitable body, registered in Scotland, with registration number SC005336.
Open this post in threaded view
|

## Re: Matlab check for negation of empty matrix

 Administrator In reply to this post by Rik-4 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
Open this post in threaded view
|

## Re: Matlab check for negation of empty matrix

 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. Sadly true. Olaf -- public key id EAFE0591, e.g. on x-hkp://pool.sks-keyservers.net signature.asc (853 bytes) Download Attachment