FIND on a 0x0 returns a 0x1

Carsten Clark tantumquantum+gnuoctave at gmail.com
Fri Mar 27 12:39:16 CDT 2009


2009/3/27 Jaroslav Hajek <highegg at gmail.com>:
>
> That's another special Matlab case missed by Octave - it seems that
> indexing by all-zeros *logical* matrices gives an empty matrix.

I think I see what you're saying, i.e. that MATLAB does this:

>> a = 1:9;
>> a(a<0)
ans =
   Empty matrix: 1-by-0
>> a = [];
>> a(a<0)  % Special case: a is 0x0, return a 0x0 rather than a vector
ans =
     []

whereas Octave 3.0.3 does this:

octave:15> a = 1:9;
octave:16> a(a<0)
ans = [](1x0)
octave:17> a = [];
octave:18> a(a<0)  % No special case.
ans = [](0x1)


So, if your code change adds MATLAB's special case behavior in the
above example, it seems to me that this would fix my complaint and
maintain your invariant.


> amazed how many ad-hoc special cases Matlab contains. So the invariant
> should hold now.

I think that your invariant, find(a) === reshape (1:numel (a), size
(a))(a != 0), already holds within Octave 3.0.3 and MATLAB, just not
between Octave and MATLAB:

function jhFindTest
    for nr = 0:2
    for nc = 0:2
        doTest ( zeros(nr,nc) )
        doTest ( ones (nr,nc) )
    end
    end
end

function doTest (m)
    inx = find(m);

    % Test JH's invariant.
    assert ( isequal (inx, jhFind(m)) )

    % Did FIND on a 0x0 return something not 0x0?
    if ( all (size(m) == 0) && ~all (size(inx) == 0) )
        inx
    end
end

function inx = jhFind (a)
    inx = reshape ( 1:numel(a), size(a) );
    inx = inx ( a ~= 0 );
end


(Octave 3.0.3)
octave:22> jhFindTest
inx = [](0x1)
inx = [](0x1)


(MATLAB)
>> jhFindTest
>>


>> not execute.  (And [tracks.active] is a row vector no matter what the
>> shape of 'tracks', right?)
>
> No, actually, it's not - when tracks is empty, it gives a 0x0 matrix
> (which is not a vector).

Oops!

> But I agree that we follow the Matlab quirks elsewhere, so we should
> follow them here.

I'm glad!

Thanks again,
Carsten



More information about the Bug-octave mailing list