[Numpy-discussion] Deleting a row from a matrix
Sebastian Haase
haase at msg.ucsf.edu
Fri Aug 25 15:48:23 CDT 2006
On Friday 25 August 2006 07:01, Travis Oliphant wrote:
> 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 0.9.9.2813 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.
>
> -Travis
>
Now of course: I often needed to "insert" a column, row or section, ... ?
I made a quick and dirty implementation for that myself:
def insert(arr, i, entry, axis=0):
"""returns new array with new element inserted at index i along axis
if arr.ndim>1 and if entry is scalar it gets filled in (ref. broadcasting)
note: (original) arr does not get affected
"""
if i > arr.shape[axis]:
raise IndexError, "index i larger than arr size"
shape = list(arr.shape)
shape[axis] += 1
a= N.empty(dtype=arr.dtype, shape=shape)
aa=N.transpose(a, [axis]+range(axis)+range(axis+1,a.ndim))
aarr=N.transpose(arr, [axis]+range(axis)+range(axis+1,arr.ndim))
aa[:i] = aarr[:i]
aa[i+1:] = aarr[i:]
aa[i] = entry
return a
but maybe there is a way to put it it numpy directly.
- Sebastian
More information about the Numpy-discussion
mailing list