Re: 3D-coordinates of index returned MAX() [message #34697 is a reply to message #34694] |
Sun, 06 April 2003 13:28   |
Mark Hadfield
Messages: 783 Registered: May 1995
|
Senior Member |
|
|
"David Fanning" <david@dfanning.com> wrote in message
news:MPG.18fa35faf111256f989b36@news.frii.com...
> Here is a reference:
>
> http://www.dfanning.com/tips/where_to_2d.html
Noting that the wheretomulti routine referred to on that page...
http://www.dfanning.com/tip_examples/wheretomulti.pro
...handles only 2D and 3D arrays, I wrote a version to handle any array
dimensionality. I called it MGH_INDN (heaven knows why). Source code is
included below my sig and there is (or will be) a copy included in the
Motley library @
http://www.dfanning.com/hadfield/README.html
--
Mark Hadfield "Ka puwaha te tai nei, Hoea tatou"
m.hadfield@niwa.co.nz
National Institute for Water and Atmospheric Research (NIWA)
--- mgh_indn.pro ---
;+
; NAME:
; MGH_INDN
;
; PURPOSE:
; Convert a 1-D array index (as returned, for example, by the
; WHERE function) to an n-dimensional index
;
; CALLING SEQUENCE:
; result = MGH_INDN(ind1, dim)
;
; POSITIONAL PARAMETERS:
; ind1 (input, compulsory, integer, scalar)
; 1-D array index
;
; dim (input, compulsory, integer, vector)
; Dimensions of array for which n-dimensional index is required.
;
; RETURN VALUE:
; The function returns an integer vector, with the same number of
; elements as the dim argument, containing indices into the
; multi-dimensional array.
;
;########################################################### ################
;
; This software is provided subject to the following conditions:
;
; 1. NIWA makes no representations or warranties regarding the
; accuracy of the software, the use to which the software may
; be put or the results to be obtained from the use of the
; software. Accordingly NIWA accepts no liability for any loss
; or damage (whether direct of indirect) incurred by any person
; through the use of or reliance on the software.
;
; 2. NIWA is to be acknowledged as the original author of the
; software where the software is used or presented in any form.
;
;########################################################### ################
;
; MODIFICATION HISTORY:
; Mark Hadfield, 2003-02:
; Written.
;-
function mgh_indn, ind1, dim
compile_opt DEFINT32
compile_opt STRICTARR
if n_elements(ind1) ne 1 then $
message, 'A single 1-D index is required'
if n_elements(dim) eq 0 then $
message, 'A list of dimensions is required'
n_dim = n_elements(dim)
result = lonarr(n_dim)
n = ind1
for i=0,n_dim-1 do begin
result[i] = n mod dim[i]
n = n / dim[i]
endfor
if n gt 0 then $
message, 'There''s some left over!'
return, result
end
|
|
|