Re: IDL Way to Remove Rows of an Array [message #75984 is a reply to message #75983] |
Tue, 10 May 2011 06:42   |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
FĂ–LDY Lajos writes:
>
> On Tue, 10 May 2011, David Fanning wrote:
>
>> Folks,
>>
>> I have other things demanding my attention this morning,
>> so I thought I would offer an IDL challenge. What is
>> the IDL Way to remove arbitrary rows of an array?
>>
>> For example, suppose I have this array:
>>
>> array = Indgen(3,6)
>> print, array
>>
>> 0 1 2
>> 3 4 5
>> 6 7 8
>> 9 10 11
>> 12 13 14
>> 15 16 17
>>
>> What is the IDL Way to remove rows 2 and 5?
>>
>> subarray = ???
>> print, subarray
>>
>> 0 1 2
>> 6 7 8
>> 9 10 11
>> 15 16 17
>
> subarray=array[*,[0,2,3,5]]
OK, here then is a function that can remove arbitrary rows
from a 2D array:
;*********************************************************** *
FUNCTION RemoveRows, array, rows
; array -- A 2D array from which rows will be removed.
; rows -- A vector of row indices to remove from array.
; Need both positional parameters.
IF N_Params() NE 2 THEN BEGIN
Print, "Usage: 'RemoveRows, array, rowsToRemove'"
RETURN, -1
ENDIF
; The array must be at least 2D.
ndims = Size(array, /N_DIMENSIONS)
IF ndims NE 2 THEN BEGIN
void = Dialog_Message('Array must be 2D.')
Print, "Usage: 'RemoveRows, array, rowsToRemove'"
RETURN, -1
ENDIF
; The rows must be a vector.
IF N_Elements(rows) EQ 1 THEN rows = [rows]
; Find the rows we are keeping.
dims = Size(array, /DIMENSIONS)
allrows = Lindgen(dims[1])
goodRows = SetDifference(allrows, rows)
; Return the subscripted array.
RETURN, array[*,goodRows]
END
;*********************************************************** *
Cheers,
David
--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.idlcoyote.com/
Sepore ma de ni thui. ("Perhaps thou speakest truth.")
|
|
|