[Numpy-discussion] matrix indexing question

Colin J. Williams cjw@sympatico...
Sun Mar 25 18:16:13 CDT 2007


Alan G Isaac wrote:
> One thing keeps bugging me when I use numpy.matrix.
> 
> All this is fine::
> 
>     >>> x=N.mat('1 1;1 0')
>     >>> x
>     matrix([[1, 1],
>             [1, 0]])
>     >>> x[1,:]
>     matrix([[1, 0]])
> 
> But it seems to me that I should be able
> to extract a matrix row as an array.

This can easily be done:
*** Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit 
(Intel)] on win32. ***
 >>> import numpy as _n
 >>> A= _n.mat([[1, 2], [3, 4]])
 >>> A[1]
matrix([[3, 4]])
 >>> A[1].getA1()
array([3, 4])

An array and a matrix are different animals.  Conversion from one to the 
other should be spelled out.

As you have done below.

Colin W.

> So this ::
> 
>     >>> x[1]
>     matrix([[1, 0]])
> 
> feels wrong.  (Similarly when iterating across rows.)
> Of course I realize that I can just ::
> 
>     >>> x.A[1]
>     array([1, 0])
> 
> but since the above keeps feeling wrong I felt I should 
> raise this as a possible design issue, better discussed
> early than latter.
> 
> Cheers,
> Alan Isaac



More information about the Numpy-discussion mailing list