Re: NumArray array-indexing

From: Christopher T King (squirrel_at_WPI.EDU)
Date: 08/16/04


Date: Mon, 16 Aug 2004 10:39:29 -0400

On 16 Aug 2004, Michael Drumheller wrote:

> However, whereas [1, 2, 3] passed as an index-array will get you the
> second, third, and fourth rows of a rank-2 matrix, [[1,2,3]] will *not*
> do that. (It gets you some other weird thing that I can't remember.)
> That is, a single-row matrix may be the same thing as a row vector
> in a mathematical context, but it is not the same thing in an
> array-indexing context. Similarly, passing [[1],
> [2],
> [3]]
> as an index array doesn't get you anything remotely like the second
> third, and fourth columns. So it seems to me that array indexing
> can easily get you an arbitrary subset of rows, but not an arbitrary
> subset of columns. Would you agree?

Forgive the poor flow of the following; I've rewritten it a couple of
times.

Think about what you're trying to do for a second. You want to pass a
list of indices to extract along a given axis. You need three pieces of
information to do this: which indices, which axis, and possibly the
geometry of the output vector. You're supplying the wrong information;
you're supplying which indices, and a geometry. Matlab guesses the right
information (which axis) from the wrong information (the geometry).

When you index a 2-dimensional array in Matlab, what happens? If the
index array is a row vector, then the indices you supply index the first
dimension. If the index array is a column vector, then the indices index
the second dimension. But what if the index array is two-dimensional?
Which dimension should the indices index? (For the record, they seem to
index the second dimension.)

numarray, on the other hand, provides an exact mechanism for supplying
index arrays thusly:

>>> a=array([[1,2,3],[4,5,6],[7,8,9]])
>>> a[[1,2],[0,0]]
array([4,7])

In this example, [1,2] are the row indices, and [0,0] are the column
indices (the information Matlab guesses for you). You can abbreviate
[0,0] as just 0:

>>> a[[1,2],0]
array([4,7])

(try the previous in Matlab; you will get similar results)

You can index along another column vector if you want:

>>> a[[1,2],1]
array([5,8])

Or you can arbitrarily index both dimensions (AFAICT, something not
possible in Matlab):

>>> a[[1,2],[0,1]]
array([4,8])

Note that doing the above in Matlab results in a 2x2 matrix, probably not
what was wanted. The explicit numarray equivalent to that Matlab
construction is a[[[1],[2]],[[0,1]]].

Note also that the output vector takes the shape of the index vector:

>>> a[[[1,2]],0]
array([[4,7]])
>>> a[[[1],[2]],0]
array([[4],
       [7]])

So the answer to your question is, yes, not only can you use arbitrary
array indices in numarray, they're more powerful than the Matlab
equivalent. Yet another reason why explicit is better than implicit.



Relevant Pages

  • Re: Matlab is not a real array oriented language.
    ... For example the language doesnt abstract arrays correctly; to matlab ... Dimension higher than 2 are handled incorrectly; ... Fortran programmers where arrays were column-wise. ... this causes programmers the need to program defensively. ...
    (comp.soft-sys.matlab)
  • Re: Matlab is not a real array oriented language.
    ... For example the language doesnt abstract arrays correctly; to matlab ... Dimension higher than 2 are handled incorrectly; ... Fortran programmers where arrays were column-wise. ...
    (comp.soft-sys.matlab)
  • Re: Suggesting improvements to Matlab
    ... Matlab - my colleagues and I are forever discussing them. ... B where each dimension of A is either the same as the ... We do have an entry in our enhancement database to consider a way to have ... function thesizes = mysize ...
    (comp.soft-sys.matlab)
  • Re: Easy codes, strange output
    ... Subscripted assignment dimension mismatch. ... I am using Matlab 6.5 ... The first time you use the code, variable 'mat' is empty. ...
    (comp.soft-sys.matlab)
  • Re: Easy codes, strange output
    ... Subscripted assignment dimension mismatch. ... I am using Matlab 6.5 ... The first time you use the code, variable 'mat' is empty. ... If your test.m is a function, then the variables are created in the local workspace of that function. ...
    (comp.soft-sys.matlab)

Loading