[Numpy-discussion] 2D Arrays column operations

Simon Berube sberub at gmail.com
Thu Mar 29 15:43:03 EDT 2007


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
since numPy arrays are more akin to C pointer this cannot be done as
easily so my question is; what is the best way to go around obtaining
the column information I want the "numPy" way? I could simply take a
transpose and get the information this way but this seems wasteful.




More information about the NumPy-Discussion mailing list