help-octave
[Top][All Lists]
Advanced

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

Re: Empty matrices


From: Francesco Potorti`
Subject: Re: Empty matrices
Date: Fri, 17 Oct 2008 09:32:48 +0200

Before answering ShunTim's request, I'd say that much of the confusion
is generated from the fact that a boolean value is displayed as "0" or
"1" rather that "true" or "false":

octave> false
ans = 0

This is very unfortunate in my opinion.  Would it be possible for Octave
to show boolean values as true and false rather than 0 and 1?

>Please enlighten.

>octave:1> a=[1, -1]
>a =
>   1  -1
>octave:2> ix=a>10
>ix =
>   0   0

ix tells you, for each element, if the condition is satisfied.  It is a
matrix of boolean (logical) values:

octave> typeinfo(ix)
ans = bool matrix
octave> whos ix

*** local user variables:

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

Total is 2 elements using 2 bytes

You can use it to choose the elements of a you want returned:

octave> a(ix)
ans = [](0x0)

You are saying that you don't want neither the first nor the second
element, so the resulting matrix is empty.

>octave:3> a(ix,1)
>ans = [](0x1)

Here you are doing a less straightforward operation.  You are indexing a
as a matrix with two rows and one column.  Consider this:

octave> a([true true],1)
error: invalid row index = 2

This is because you are asking for an element in the second row, which
is not there.

octave> a([true false],1)
ans =  1

Here you get no error, because access to the second row is never tried
(I would have said that an error should be generated here, to be true).

octave> a([false false],1)
ans = [](0x1)

Consequently, you get no error here either, because no row is ever
accessed, so you get a "no rows" by "1 column" matrix.

>octave:4> a(0,1)
>error: invalid row index = 0

This is because 0 is a number, a row index.  It is not a boolean value.
Contrast it with:

octave> a(false,1)
ans = [](0x1)

>And I don't see how to create it:

As in the above example, or in one of these ways:

octave> zeros(0,1)
ans = [](0x1)
octave> reshape([],0,1)
ans = [](0x1)

-- 
Francesco Potortì (ricercatore)        Voice: +39 050 315 3058 (op.2111)
ISTI - Area della ricerca CNR          Fax:   +39 050 315 2040
via G. Moruzzi 1, I-56124 Pisa         Email: address@hidden
(entrance 20, 1st floor, room C71)     Web:   http://fly.isti.cnr.it/


reply via email to

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