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.
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])
c = a[:,2] -Travis
Awww, this is quite right. I kept using the a[0][:] notation and I
assume I am simply pulling out single arrays from the array "list".
Thank you very much for the prompt reply. (And sorry for wasting your
time :P)
On Mar 29, 3:46 pm, Travis Oliphant
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])
c = a[:,2]
-Travis
_______________________________________________ Numpy-discussion mailing list Numpy-discuss...@scipy.orghttp://projects.scipy.org/mailman/listinfo/numpy-discussion
Others can correct me, but I believe the ndarrys of
numpy are *not* stored like C arrays, but rather as a
contiguous memory chunk (there are some exceptions,
but ignore for the moment). Then along with the data
is a structure that tells numpy how to "address" the
array's data memory using indices (+ other array
info). In particular, the structure tells numpy the
dimension and the strides for each dimension so it can
skip along and pick out the right components when you
write c=a[:,1].
I'm sure Travis will correct me if I'm off, but I
think this is basically how numpy arrays operate.
--- Simon Berube
Awww, this is quite right. I kept using the a[0][:] notation and I assume I am simply pulling out single arrays from the array "list".
Thank you very much for the prompt reply. (And sorry for wasting your time :P)
-- Lou Pecora, my views are my own. --------------- "I knew I was going to take the wrong train, so I left early." --Yogi Berra ____________________________________________________________________________________ Be a PS3 game guru. Get your game face on with the latest PS3 news and previews at Yahoo! Games. http://videogames.yahoo.com/platform?platform=120121
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
participants (4)
-
Lou Pecora
-
Simon Berube
-
Stefan van der Walt
-
Travis Oliphant