[Numpy-discussion] Deleting a row from a matrix

Travis Oliphant oliphant.travis at ieee.org
Fri Aug 25 09:01:36 CDT 2006

Keith Goodman wrote:
> How do I delete a row (or list of rows) from a matrix object?
> To remove the n'th row in octave I use x(n,:) = []. Or n could be a
> vector of rows to remove.
> In numpy x[[1,2],:] = [] changes the values of all the
> elements of x without changing the size of x.
> In numpy do I have to turn it around and construct a list of the rows
> I want to keep?
Basically, that is true for now.  

I think it would be worth implementing some kind of function for making 
this easier.

One might think of using:

del a[obj]

But, the problem with both of those approaches is that once you start 
removing arbitrary rows (or n-1 dimensional sub-spaces) from an array 
you very likely will no longer have a chunk of memory that can be 
described using the n-dimensional array memory model.

So, you would have to make memory copies.  This could be done, of 
course, and the data area of "a" altered appropriately.  But, such 
alteration of the memory would break any other objects that have a 
"view" of the memory area of "a."  Right now, there is no way to track 
which objects have such "views", and therefore no good way to tell 
(other than the very conservative reference count) if it is safe to 
re-organize the memory of "a" in this way.

So, "in-place" deletion of array objects would not be particularly 
useful, because it would only work for arrays with no additional 
reference counts (i.e. simple b=a assignment would increase the 
reference count and make it impossible to say del a[obj]).

However, a function call that returned a new array object with the 
appropriate rows deleted (implemented by constructing a new array with 
the remaining rows) would seem to be a good idea. 

I'll place a prototype (named delete) to that effect into SVN soon.


More information about the Numpy-discussion mailing list