[Numpy-discussion] Deleting a row from a matrix

Travis Oliphant oliphant.travis at ieee.org
Fri Aug 25 10:01:58 CDT 2006


Travis Oliphant wrote:
>> 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
>>   
>>     
>
> Sure, it makes sense to parallel the delete function.
>   
Although there is already and insert function present in numpy....

-Travis





More information about the Numpy-discussion mailing list