help-octave
[Top][All Lists]
Advanced

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

Re: Empty matrices


From: Jordi Gutiérrez Hermoso
Subject: Re: Empty matrices
Date: Fri, 17 Oct 2008 01:42:22 -0500

2008/10/16 LUK ShunTim <address@hidden>:
> This is something that I don't understand. Consider this:
>
> octave:1> a=[1, -1]
> a =
>
>   1  -1
>
> octave:2> ix=a>10
> ix =
>
>   0   0
>
> octave:3> a(ix,1)
> ans = [](0x1)
>
> I don't understand the (0x1) after the empty matrix.

It might help if you use whos to see what kind of data you have here:

     octave:31> whos ix

     *** local user variables:

        Prot Name        Size                     Bytes  Class
        ==== ====        ====                     =====  =====
        rwd ix          1x2                          2  logical

     Total is 2 elements using 2 bytes

Here you see that ix is 1x2 matrix of logical falses, or bools (which
happen to look like zeroes but aren't). Instead, if you do ix = [0 0]
and you whos that, you'll see that it's a 1x2 vector of doubles, not
bools.

When you index by a vector of bools, what you are saying is, "output a
vector that only includes the logically true entries from the bool
indexing vectors".

There's a further catch. When you index with a bool matrix, it
flattens into a column vector in the first entry and a row vector in
the second entry. What you are requesting with a(ix,1) the first
column of neither row, hence a 0x1 matrix (one column, no rows).
Contrariwise, if you do a(1,ix), you are requesting the first row of
neither column, so you'll get a one-row, no-column matrix.

It's also not hard to build an empty 0xN matrix for any N:

     a = zeros(N);
     for i = 1:N
          a(1,:) = [];
     endfor

This loop removes all the rows of the matrix until you get zero rows
but N columns.

HTH,
- Jordi G. H.


reply via email to

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