|
Re: 3D-coordinates of index returned MAX() [message #34681 is a reply to message #34667] |
Mon, 07 April 2003 13:32  |
Mark Hadfield
Messages: 783 Registered: May 1995
|
Senior Member |
|
|
"Marc Schellens" <m_schellens@hotmail.com> wrote in message
news:3E916642.1070904@hotmail.com...
> I wote something similar to Mark, but here you convert all
> indices at once.
Great, I'll steal that idea.
> And instead of a list of dimensions, you just
> call it with the array which was indexed by the 'where' function.
Yeah, I thought about that, but I figured there *might* be a situation where
one wants to calculated n-dimensional indices without having (or wanting to
create) an array with that dimensionality. ... I haven't actually
encountered this situation however.
--
Mark Hadfield "Ka puwaha te tai nei, Hoea tatou"
m.hadfield@niwa.co.nz
National Institute for Water and Atmospheric Research (NIWA)
|
|
|
Re: 3D-coordinates of index returned MAX() [message #34694 is a reply to message #34681] |
Mon, 07 April 2003 04:51  |
marc schellens[1]
Messages: 183 Registered: January 2000
|
Senior Member |
|
|
I wote something similar to Mark, but here you convert all
indices at once. And instead of a list of dimensions, you just
call it with the array which was indexed by the 'where' function.
Hope it helps,
marc
;; NAME:
;; L_GetDim
;;
;; PURPOSE:
;; translates a one-dimensional index (like given by where() function)
;; into a multidimensional one (i.e. the array indices according to
;; the multidimensional array)
;;
;; PARAMETERS:
;; a the array
;; ix the one dimensional index (or array of indices)
;; if ix is omitted, the dimensions of a are returned
;;
;; KEYWORDS:
;; MINDIM if set, only the number of dimensions of a is returned,
;; else 8 dimensions (what is better in some degenerated
;; cases, i.e the calling program can rely on that there is
;; always a second(third...) dimension given)
;;
;; returns a 8 by n_elements(ix) array
;;
;; example:
;;IDL> a=intarr(23,24,27,33)
;;IDL> a[13,19,2,11]=1
;;IDL> ix=where(a)
;;IDL> print,l_getdim(a,ix)
;; 13 19 2 11 0
0 0 0
;;IDL> print,l_getdim(a)
;; 23 24 27 33 1
1 1 1
;;
;; MODIFICATION HISTORY:
;; Marc Schellens 01.2002
function L_GetDim,a,ix,MINDIM=minDim
sz=size(a)
if n_params() eq 1 then begin
;; maximum of eight dimensions in IDL
if keyword_set(minDim) then return,size(a,/dim)
r=lonarr(8)
r[*]=1
if sz[0] ge 1 then r[0:sz[0]-1]=sz[1:sz[0]]
return,r
endif
nConv=n_elements(ix)
;; maximum of eight dimensions in IDL
r=lonarr(keyword_set(minDim)?sz[0]>1:8,nConv)
;; index 1
r[0,*]=ix mod sz[1]
;; index 2..n-1
sum=1L
for i=1,sz[0]-2 do begin
sum=sum*sz[i]
r[i,*]=(ix / sum) mod sz[i+1]
endfor
;; index n
if sz[0] ge 2 then begin
i=sz[0]-1
sum=sum*sz[i]
r[i,*]=ix / sum
endif
return,r
end
|
|
|
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
|
|
|
Re: 3D-coordinates of index returned MAX() [message #34698 is a reply to message #34697] |
Sun, 06 April 2003 13:03  |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
Alexander Gro� (AlexanderGross_NOSPAM@gmx.de) writes:
> I have problems determining the coordinates from the index returned by
> MAX(). The online help has an example for that in 2D, but I need a solution
> for 3D-space.
>
> What tells me this index? Is something like the pixelnumber in the whole
> 3D-array? How is this array referenced through the index?
>
> The code looks like this:
> nXCoord = nMaxIndex mod size_imgRef_x
> nYCoord = nMaxIndex / (size_imgRef_x * size_imgRef_z)
> nZCoord = nMaxIndex / (size_imgRef_x * size_imgRef_y)
>
> It does not work for me. Am I just to blind/stupid to see the solution or is
> there some difference between 2D and 3D.
>
> Best regards and thanks for your help in advance,
Your Y index is wrong. It should be:
nYCoord = (nMaxIndex/size/imgRef_x) MOD size_imgRef_y
Here is a reference:
http://www.dfanning.com/tips/where_to_2d.html
Cheers,
David
--
David W. Fanning, Ph.D.
Fanning Software Consulting, Inc.
Phone: 970-221-0438, E-mail: david@dfanning.com
Coyote's Guide to IDL Programming: http://www.dfanning.com/
Toll-Free IDL Book Orders: 1-888-461-0155
|
|
|