help-octave
[Top][All Lists]
Advanced

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

Re: Matrix index, Please e-mail back.


From: Paul Kienzle
Subject: Re: Matrix index, Please e-mail back.
Date: Sat, 16 Dec 2006 18:48:45 -0500


On Dec 16, 2006, at 6:37 AM, Joan Picanyol i Puig wrote:

* Paul Kienzle <address@hidden> [20061206 03:36]:

On Dec 5, 2006, at 6:39 PM, Jordi Gutierrez Hermoso wrote:

There are cute little indexing hacks that can be accomplished if you
remember that Octave stores matrices in Fortran column-major order. In
other words, a matrix can be indexed by a single index as if it were
one long vector.

The functions sub2ind and ind2sub hide the details, letting you
access an array element by element rather than by slices.

Could you please share some hints as how are they to be used? The docs
are a bit too concise for my taste: what is "DIMS" supposed to be? What
about "S1, ..., SN"? I've started to use indexing heavily as of late,
but have been unable to use sub2ind and ind2sub, I tipically get an
"index out of range" error (see
http://www.cae.wisc.edu/pipermail/help-octave/2006-October/001983.html )

 octave> z=reshape(1:3*4*2,[3,4,2])
 z =

 ans(:,:,1) =

   1   4   7  10
   2   5   8  11
   3   6   9  12

 ans(:,:,2) =

  13  16  19  22
  14  17  20  23
  15  18  21  24


Let's pick out elements (1,3,1)=7 and (2,2,2)=17

 octave> z(sub2ind(size(z), [1 2],[3 2],[1 2]))
 ans =

   7  17

Let's find the elements containing 11<z<15

 octave> [i,j,k] = ind2sub(size(z),find(z>11 & z<15))
 i =

  3
  1
  2

 j =

  4
  1
  1

 k =

  1
  2
  2

- Paul



reply via email to

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