[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Where is the largest point ?



There's a bit of black magic regarding how IDL treats arrays.  Any
multi-dimensional array can be treated as a 1-d array and in
a number of contexts IDL always treats arrays that way.


Suppose you have a two-D 500x400 array.  When you use
it in the max function, IDL treats it as a 1-D 200000 element
array, so you just get back a scalar value for maxIndex.
To get the 2-d indices just use:

   ix = maxIndex % 500
   iy = maxIndex / 500  -- note that we use the X size in both lines.

The same thing happens with the where function.
E.g., suppose you want to know all of the values where your array
is 0.

  w = where(array eq 0)

Now w is an array of the indices into the array but again treating
it as a 1-D array.  [It just has a single element -1 if there are no values satisfying
the criteria].  Often its fine to just access the array as if it were 1-d, e.g,

   array[w] = 0.1

but if you need to get the x and y indices (e.g., in a distance calculation),
then you want to do

   px = w%nx
   py = w/nx 

where nx is the number of elements in a single line.

Now px and py will be arrays with the same length as w and you
can use array[w] or array[px,py] interchangeably.  

Hope this helps more than it confuses,

	Good luck,
	Tom McGlynn

Ben Tupper wrote:
> 
> Hi,
> 
> Take a peek at the MAX function (check the online help)
> 
> maxVal = Max(Arrat, maxIndex)
> 
> maxVal will return the maximum value
> maxIndex will return the index of the maximum value within the array.
> If there is more than one value qualifying as MAX, then only the index
> of the first maxVal encountered is returned in maxIndex.
> 
> Ben
> 
> Guillaume Dargaud wrote:
> >
> > I'm still pretty basic at IDL and a lot of the _no loop_ concepts escape me
> > entirely (I'm too used to C and such).
> > If I have a 2D matrix, how do I find where is the maximum ?
> > Say:
> >
> > Mat=FltArr(NbX, NbY)
> > ...
> > [Xmax, YMax] = Where( Mat eq Max(Mat) )
> >
> > or something like that ?
> > --
> > Guillaume Dargaud
> > Colorado State University - Dept of Atmospheric Science
> > http://rome.atmos.colostate.edu/
> >   "If those folks in Kansas are right about evolution never having happened,
> > I sure hope it happens soon."    - Michael Sheinbaum.
> 
> --
> Ben Tupper
> Bigelow Laboratory for Ocean Sciences
> 180 McKown Point Rd.
> W. Boothbay Harbor, ME 04575
> btupper@bigelow.org