# [Numpy-discussion] matrix indexing question

Zachary Pincus zpincus@stanford....
Mon Mar 26 15:29:26 CDT 2007

```> Since matrices are an iterable Python object,
> we *expect* to iterate over the contained objects.
> (Arrays.)  I am not sure why this is not evident to all,
> but it is surely the sticking point in this discussion.
>
> A matrix is not a container of matrices.
> That it acts like one is surprsing.
> Surprises are bad unless they have a clear justification.
> Perhaps a clear justification exists,
> but it has not been offered in this discussion.

I think that a clear justification has been offered several times on
the list recently, though maybe not all in this thread.

Matrices in numpy seem to exist as a construct to facilitate linear
algebra. One such property is that row- and column-vector slices of
matrices are (1xN) or (Nx1) matrices, because otherwise common linear
algebra things -- like how you multiply a row-vector or a column
vector by a matrix, and whether and when it needs to be transposed --
do not translate properly from "linear algebra notation" like in
textbooks and papers.

Once the matrix class is committed to providing row- and column-
vector slices as other matrices, it makes no sense to have iteration
over the matrix provide a different data type than slicing.

Basically, my rule of thumb has been to *only* use matrices when I'm
copying linear algebra operations out of textbooks and papers, and I
want the notations to align. Doing other, non-linear-algebra
operations with matrices -- like iterating over their elements -- is
not really worth it.

There's a meta question, of course: should things be changed to make
it "worth it" to do "pythonic" tasks with matrices? Should there be
special elementwise vs. matrix-operation operators? Should numpy work
a lot more like matlab? That discussion is on-going in another