[Numpy-discussion] matrix indexing question

Alan G Isaac aisaac@american....
Sun Mar 25 21:26:24 CDT 2007

>> On 3/26/07, Alan G Isaac <aisaac@american.edu> wrote: 
>>> finds itself in basic conflict with the idea that 
>>> I ought to be able to iterate over the objects in an 
>>> iterable container.  I mean really, does this not "feel" 
>>> wrong? ::
    >>> for item in x: print item.__repr__()
    >>> ...
    >>> matrix([[1, 2]])
    >>> matrix([[3, 4]]) 

On Mon, 26 Mar 2007, Bill Baxter apparently wrote: 
> So you're saying this is what you'd find more pythonic? 
>>>> X[1] 
> matrix([2,3]) 
>>>> X[:,1] 
> matrix([[3, 
>             4]]) 
> Just trying to make it clear what you're proposing. 

No; that is not possible, since a matrix is inherently 2d.
I just want to get the constituent arrays when I iterate
over the matrix object or use regular Python indexing, but 
a matrix when I use matrix/array indexing.  That is ::

    >>> X[1] 
    >>> X[1,:] 
    matrix([[3, 4]]) 

That behavior seems completely natural and unsurprising.

> Probably about half the bugs I get from mixing and matching matrix and 
> array are things like 
>    row = A[i] 
>    ... 
>    z = row[2]
> Which works for an array but not for a matrix. 

That is the evidence of a "bad surprise" in the current 
behavior.  Iterating over a Python iterable should provide 
access to the contained objects.

Alan Isaac

More information about the Numpy-discussion mailing list