Fanning Software Consulting

Remove Rows the IDL Way

QUESTION: I guess I can figure out how to remove rows of a 2D array with loops and array subscripting, but the code is not the least bit elegant. Can you show me the IDL Way to remove rows from a 2D array?

ANSWER: Sure. This answer comes courtesy of JD Smith.

Consider the following 2D array.

   IDL> array = Indgen(3, 6)
   IDL> Print, array
       0       1       2
       3       4       5
       6       7       8
       9      10      11
      12      13      14
      15      16      17

Suppose we wish to remove the 2nd and 4th rows, given by the indices 1 and 3. We could do it like this.

   IDL> dims = Size(array, /Dimensions)
   IDL> rows = [1,3]
   IDL> shortArray = array[*, Where(~Histogram(rows, MIN=0, MAX=dims[1]-1), /NULL)]
   IDL> Print, shortArray
       0       1       2
       6       7       8
      12      13      14
      15      16      17

You can find a short program, named RemoveRows, that will implement this algorithm.

   IDL> Print, RemoveRows(array, rows)
       0       1       2
       6       7       8
      12      13      14
      15      16      17

Version of IDL used to prepare this article: IDL 7.1.1.

Written: 17 May 2011