what is the highest subscript in the array?!? [message #70774] |
Thu, 06 May 2010 19:12  |
munka
Messages: 36 Registered: December 2009
|
Member |
|
|
Hello team,
I find myself wanting to use the highest subscript in an array and
coding "flux[n_elements(flux)-1]"... I seem to remember seeing a
shortcut on here, and I can't remember what it is.
It's irrelevant, but here is where I most recently used this. This
finds the local maximum in an array.
index=where(flux eq max(flux) and flux ne flux[0] and flux ne
flux[n_elements(flux)-1],ct)
Thanks in advance,
~Bill
PS: Right before I posted this, I searched and I figured that
array[[-1]] should return the highest array value... but it doesn't,
and I'm still stumped.
IDL> array=indgen(10)
IDL> print,array[0]
0
IDL> print,array[9]
9
IDL> print,array[10]
% Attempt to subscript ARRAY with <INT ( 10)> is out of
range.
% Execution halted at: $MAIN$
IDL> print,array[[10]]
9
IDL> print,array[[-1]]
0
IDL> print,array[[-2]]
0
IDL>
|
|
|
|
Re: what is the highest subscript in the array?!? [message #70849 is a reply to message #70774] |
Fri, 07 May 2010 12:05   |
penteado
Messages: 866 Registered: February 2018
|
Senior Member Administrator |
|
|
On May 7, 3:46 pm, munka <mynameismu...@gmail.com> wrote:
>> On May 7, 6:20 am, wlandsman <wlands...@gmail.com> wrote:
>> Sorry, that should be
>
>> dum = max(flux[1:N_elements(flux)-2], c) & index = c+1
>
> That still does not return the LOCAL maximum. If the max is on the
> edge, it will still return a value
>
> IDL> flux=findgen(10)
> IDL> print,flux
> 0.00000 1.00000 2.00000 3.00000 4.00000
> 5.00000
> 6.00000 7.00000 8.00000 9.00000
> IDL> print,max(flux)
> 9.00000
> IDL> print,max(flux[1:N_elements(flux)-2], c)
> 8.00000
> IDL> print,c+1
> 8
Could do something like
nflux=n_elements(flux)
local_maxima=where((flux[1:nflux-2] ge flux[2:nflux-1]) and
(flux[1:nflux-2] ge flux[0:nflux-3]))+1
That would give the indexes of all points that are local maxima,
defined as those larger than or equal to their immediate neighbors.
Then max() or histogram() may be used to pick the highest maxima,
depending on what is wanted.
|
|
|
Re: what is the highest subscript in the array?!? [message #70850 is a reply to message #70774] |
Fri, 07 May 2010 11:51   |
munka
Messages: 36 Registered: December 2009
|
Member |
|
|
On May 7, 10:37 am, eddie <eha...@gmail.com> wrote:
>> PS: Right before I posted this, I searched and I figured that
>> array[[-1]] should return the highest array value... but it doesn't,
>> and I'm still stumped.
>
> Add a 'u' and it should. A negative unsigned number "wraps" to
> produce a large number.
>
> IDL> array = indgen(10)
> IDL> print,array[[-1u]]
> 9
> This is similar to Carsten's huge number as an index. You can use
> array[[-1ull]] if you are concerned that your array might have more
> than a gajillion elements.
>
> Unfortunately this trick only works for the last element, array[[-2u]]
> still returns the last element of the array, not the second to last.
>
> Cheers,
> eddie
IDL> flux=findgen(10)
IDL> print,flux
0.00000 1.00000 2.00000 3.00000 4.00000
5.00000
6.00000 7.00000 8.00000 9.00000
IDL> print,flux[[-1u]]
9.00000
IDL> print,flux[5:*]
5.00000 6.00000 7.00000 8.00000 9.00000
Yes! The -1u works! I think I remembered the "trick" that I was
originally thinking of. It doesn't do what I want it to do, but the
[[-1u]] works!
Thanks for the responses!
~Bill
|
|
|
Re: what is the highest subscript in the array?!? [message #70851 is a reply to message #70774] |
Fri, 07 May 2010 11:46   |
munka
Messages: 36 Registered: December 2009
|
Member |
|
|
> On May 7, 6:20 am, wlandsman <wlands...@gmail.com> wrote:
> Sorry, that should be
>
> dum = max(flux[1:N_elements(flux)-2], c) & index = c+1
That still does not return the LOCAL maximum. If the max is on the
edge, it will still return a value
IDL> flux=findgen(10)
IDL> print,flux
0.00000 1.00000 2.00000 3.00000 4.00000
5.00000
6.00000 7.00000 8.00000 9.00000
IDL> print,max(flux)
9.00000
IDL> print,max(flux[1:N_elements(flux)-2], c)
8.00000
IDL> print,c+1
8
|
|
|
|
|
|
|
|
|