faqts : Computers : Programming : Languages : Python : Modules : numpy

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

1 of 3 people (33%) answered Yes
Recently 0 of 2 people (0%) answered Yes

Entry

How do I create a copy an array with specified entries removed (like slicing in reverse)? The Matlab way of doing it is: X1=X0(:,[1:j-1,j+1:p])

Mar 30th, 2006 12:19
Dan Schult, Louis Luang,


numpy accepts a list/tuple for slice indices, so if you can create a list/tuple of
the specified entries you want, the syntax is very similar to that for matlab.

For the example you show, the numpy syntax is:
X1=X0[:, [ i for i in range(p) if i !=j] ]
or perhaps more to your liking:
X1=X0[:, range(j-1)+range(j,p)]

Remember that indices start counting at 0, 
so if j==5 this will remove the 6th column of the array.

As another example, if you only wanted every other row:
X1=X0[ range(0,p,2) , :]