[Numpy-discussion] 2D Arrays column operations

Stefan van der Walt stefan@sun.ac...
Thu Mar 29 14:51:28 CDT 2007


On Thu, Mar 29, 2007 at 07:43:03PM -0000, Simon Berube wrote:
> Hi,
>    I am relatively new to Python/NumPy switching over from Matlab and
> while porting some of my matlab code for practice I ran into the
> following problem.
> 
> Assume we have a 2D Matrix such that
> a = array([[1, 2, 3],
>                [4, 5, 6],
>                [7, 8, 9]])
> 
> If I want the second row I can simply enough take
> 
> c = a[1]
> 
> However, I would like to do a similar operation on the columns of the
> 2D Array. In matlab I could simply do
> 
> c = a(:,2) to get the values array([2,5,8])
> 
> In numPy this seems to not be a valid operation. I understand that

Not?

In [2]: a = array([[1, 2, 3],
   ...:                [4, 5, 6],
   ...:                [7, 8, 9]])

In [3]: a[:,1]
Out[3]: array([2, 5, 8])

Cheers
Stéfan


More information about the Numpy-discussion mailing list