help-octave
[Top][All Lists]
Advanced

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

Re: Indexing confusion


From: John W. Eaton
Subject: Re: Indexing confusion
Date: Fri, 02 May 2008 12:55:42 -0400

On  2-May-2008, Bill Denney wrote:

| > It's expected.  Indexing a matrix with another single matrix produces
| > a result the same size as the index.  Elements of the matrix and index
| > are accessed in column-major order.  Note the difference between
| > X(1,2) and X([1,2]).  To get the result you want, try
| >
| >   subs = fullfact (size (X));
| >   X(sub2ind (size (X), subs(:,1), subs(:,2)))
| >   
| An alternative that I find more readable is:
| 
| subs = fullfact (size (X));
| X(subs(:,1), subs(:,2))
| 
| I'm not positive that works (haven't tested it), but I think it does, 
| and to me it is more readable.

It works, but it selects an intersection of the specified rows and
columns, which is different from what the sub2ind indexing does.
Compare

  x = hilb (10);
  x(sub2ind (size (x), [1,2,3], [4,5,6]))
  x([1,2,3], [4,5,6])

The first selects the elements x(1,4), x(2,5), and x(3,6).  The second
selects the intersection of rows [1,2,3] with the columns [4,5,6].

jwe


reply via email to

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