Thursday, October 16, 2014

Hooray, Octave supports numpy-style boolean array indexing operations

I've only just discovered that Gnu Octave (the Gnu version of Matlab) supports numpy-style boolean array indexing operations, in particular

  • logical operations on vectors to return boolean true/false vectors
  • array indexing using vectors on vectors
An example, using a simple vector a.
octave:2> a=[1 2 3 4 1 2 3 4]
a =

   1   2   3   4   1   2   3   4

Create a boolean mask called b with all elements of a greater than 2.
octave:3> b=a > 2
b =

   0   0   1   1   0   0   1   1
Now use b to access only those elements of a that are true in the mask array b.
octave:4> a(b)
ans =

   3   4   3   4
Why use Octave when we have python/numpy/scipy? Sometimes its just faster to fire up octave to get a look at data, and matlab/octave syntax is much less verbose than python.