next up previous contents
Next: Defining functions and procedures Up: Array operations Previous: The where function   Contents

wheretomulti

There is a handy routine (wheretomulti.pro) to convert these 1d subscripts to rows, columns etc in a 2D or 3D array (again from David Fanning). IDL 6.0 also has a new function I think, called array_indices, which is related to this.

PRO wheretomulti, Array, Indices, Col, Row, Frame
;+
; NAME:		wheretomulti.pro
;
; FUNCTION:	Convert WHERE output to 2d or 3d indices
;
; USAGE:	WhereToMulti, Array, Indices, Col, Row, Frame
;
; INPUT ARGUMENTS: 
;   Array: the array that was WHERE'd
;   Indices: the indices returned by WHERE
;
; OUTPUT ARGUMENTS: 
;   Col:     Indices to first dimension.
;   Row:     Indices to second dimension.
;   Frame:   Indices to third dimension. Returned only for 3-d array.
;
; OPTIONAL ARGUMENTS: 
;
; KEYWORDS: 
;
; REQUIRED MODULES: 
;
; SIDE EFFECTS: 
;
; ERROR HANDLING:
;   If Array is not a vector or matrix, all return values are set to zero
;   and a message is written to the screen.
;
; NOTES:
;
; HISTORY:
; 1998 Sept 15	J.L.Saba	Developed based on code from David Fanning's
;                               web site.
;
;- End of prologue -------------------------------------------------------------

;Find size of input array
   s    = SIZE ( Array )
;Size returns n_dimesnions, size_dimension(1),  size_dimension(2) etc
;Size returns 0 dimensions if the variable does not exist.                         

;Number of columns of array =s[1]     
   NCol = s[1]

;The keyword MOD is the modulo operator. I MOD J is equal to the
;remainder when I is divided by J.  
;Col(i) is column that Indices(i) refers to
   Col  = Indices MOD NCol
;since the indices count along the columns working up the rows

   IF s[0] EQ 2 THEN BEGIN              ; 2-d array
      Row = Indices / NCol ;Integer/integer=integer - so wed on't get a fractional row
   ENDIF ELSE IF s[0] EQ 3 THEN BEGIN   ; 3-d array
      NRow  = s(2)
      Row   = ( Indices / NCol ) MOD NRow
      Frame = Indices / ( NRow * NCol )
   ENDIF ELSE BEGIN                     ; neither 2d or 3d
      Col   = 0
      Row   = 0
      Frame = 0
      PRINT, 'WhereToMulti called with bad input. Array not a vector or matrix.'
      HELP, Array
   ENDELSE

   RETURN
END



John Marsham 2005-04-22