[Numpy-discussion] matrix indexing question

Bill Baxter wbaxter@gmail....
Sun Mar 25 19:10:34 CDT 2007


On 3/26/07, Colin J. Williams <cjw@sympatico.ca> wrote:
> Bill Baxter wrote:
> > This may sound silly, but I really think seeing all those brackets is
> > what makes it feel wrong.   Matlab's  output doesn't put it in your
> > face that your 4 is really a matrix([[4]]), even though that's what it
> > is to Matlab.  But I don't see a good way to change that behavior.
> >
> > The other thing I find problematic about matrices is the inability to
> > go higher than 2d.  To me that means that it's impossible to go "pure
> > matrix" in my code because I'll have to switch back to arrays any time
> > I want more than 2d (or use a mixed solution like a list of matrices).
> >  Matlab allows allows >2D.
> >
> > --bb
> "pure matrix" seems to me an area of exploration, does it have any
> application in numerical computation at this time?

I'm not sure what you thought I meant, but all I meant by going "pure
matrix" was having my Numpy code use the 'matrix' type exclusively
instead of some mix of 'matrix' and the base 'ndarray' type.  Things
become messy when you mix and match them because you don't know any
more if an expression like A[1] is going to give you a 1-D thing or a
2-D thing, and you can't be sure what A * B will do without always
coercing A and B.

> A list of matrices seems to be a logical structure.

Yes, and it's the only option if you want to make a list of matrices
of different shapes, but I frequently have a need for things like a
list of per-point transformation matrices.  Each column from each of
those matrices can be thought of as a vector.  Sometimes its
convenient to consider all the X basis vectors together, for instance,
which is a simple and efficient M[:,:,0] slice if I have all the data
in a 3-D array, but it's a slow list comprehension plus matrix
constructor if I have the matrices in a list -- something like
    matrix([m[:,0] for m in M])
but that line is probably incorrect.

> PyMatrix deals with
> lists in building a larger matrix from sub-matrices.
>
> Suppose that we have matrices A (3, 4), B (3, 6), C (4, 2) and D (4, 8).
>
> Then E= M([[A, B], [C, D]]) gives E (7, 10).

Numpy generally tries to treat all lists and tuples as array literals.
 That's not likely to change.

--bb


More information about the Numpy-discussion mailing list