Logical indexing and Octave manual example

John W. Eaton jwe at bevo.che.wisc.edu
Wed Mar 5 19:17:20 CST 2008


On  5-Mar-2008, Tim Largy wrote:

| I'm having trouble understanding an example in section 4.4 "Logical
| Values" of the Octave manual
| (http://www.gnu.org/software/octave/doc/interpreter/Logical-Values.html#Logical-Values).
| The manual has:
| 
|      data = [ 1, 2; 3, 4 ];
|      idx = (data <= 2);
|      data(idx)
|           => ans = [ 1; 4 ]

That looks like an error in the manual.  The result should be [ 1; 2 ].

| However my Octave version 2.1.73 (i686-pc-cygwin) returns a different result:
| 
|     octave:72>  data = [ 1, 2; 3, 4 ];
|     octave:73>  idx = (data <= 2);
|     octave:74>  data(idx)
|     ans =
| 
|        1
|        2
| 
| In neither case can I understand the underlying logic. The value of
| idx is [1, 1; 0, 0]. What rules determine how a matrix such as this is
| used in logical indexing?

The values are logical (true/false).  So the index selects acts as a
mask and selects elements that correspond to true values in the mask.

Just above the example you quote, it says

  Logical values can also be used to index matrices and cell
  arrays.  When indexing with a logical array the result will be a
  vector containing the values corresponding to true parts of the
  logical array.

Perhaps it should say "true elements" and/or use the word mask?

jwe


More information about the Help-octave mailing list