[Numpy-discussion] matrix indexing question

Christopher Barker Chris.Barker@noaa....
Tue Mar 27 12:47:56 CDT 2007

Zachary Pincus wrote:
> rest of linear algebra -- e.g. that m[0] yields a matrix if m is a  
> matrix-- it almost certainly would violate the principle of least  
> surprise for iteration over m (intuitively understood to be choosing m 
> [0] then m[1] and so forth) to yield anything other than a matrix.

I don't think the OP was suggesting that. Rather, I think the suggestion 
was that for a  mXn matrix, M:

M[i].shape == (n,)

M[i:i+1].shape == (1,n)

that is, indexing (or iterating returns a vector, and slicing returns a 
matrix). This is, indeed exactly how numpy arrays behave!

The problem with this is:

numpy matrices were created specifically to support linear algebra 
calculations. For linear algebra, the distinction between row vectors 
and column vectors is critical. By definition, a row vector has shape: 
(1,n), and a column vector has shape (m,1).

In this case, perhaps the OP is thinking that a shape (n,) array could 
be considered a row vector, but in that case:

M[1,:].shape == (n,)
M[:,1].shape == (m,)

which of these is the row and which the column? This is why matrices 
index this way instead:

M[1,:].shape == (1, n)
M[:,1].shape == (m, 1)

now we know exactly what is a row and what is a column.

By the way, I think with the way numpy works: M[i] == M[i,:] by 
definition, so those couldn't yield different shaped results. Is that right?

I think we got a bit sidetracked by the example given. If I have a bunch 
of points I want to store, I'm going to use an (m,2) *array*, not a 
matrix, then then A[i] will yield a (2,) array, which makes sense for 
(2-d) points. In fact, I do this a LOT.

If I happen to need to do some linear algebra on that array of points, 
I'll convert to a matrix, do the linear algebra, then convert back to an 
a array (or just use the linear algebra functions on the array).

I hope this helps


> This can't possibly be what you're asking for, right? You aren't  
> suggesting that m[0] and list(iter(m))[0] should be different types?
> There are many clear and definite use cases for m[0] being a matrix,  
> by the way, in addition to the theoretical arguments -- these aren't  
> hard to come by. Whether or nor there are actual good use-cases for  
> iterating over a matrix, if you believe that m[0] should be a matrix,  
> it's madness to suggest that list(iter(m))[0] should be otherwise.
> My opinion? Don't iterate over matrices. Use matrices for linear  
> algebra. There's no "iteration" construct in linear algebra. The  
> behavior you find so puzzling is a necessary by-product of the  
> fundamental behavior of the matrix class -- which has been explained  
> and which you offered no resistance to.
> Zach
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion@scipy.org
> http://projects.scipy.org/mailman/listinfo/numpy-discussion

Christopher Barker, Ph.D.

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception


More information about the Numpy-discussion mailing list