comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » Locating desired values in an array.
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Switch to threaded view of this topic Create a new topic Submit Reply
Locating desired values in an array. [message #89093] Wed, 23 July 2014 18:50 Go to next message
gpeterso is currently offline  gpeterso
Messages: 22
Registered: February 2013
Junior Member
So i have an array that is [2,58]. I need the number that is closet to -42 for each time the track number increases by one. So for example for track number 28742 I should get the third bin number of -42.6996. I am very confused on how to do approach this.



lat track number
-45.9996 28742.0
-44.3502 28742.0
-42.6996 28742.0
-41.0498 28742.0
-39.3996 28742.0
-44.5354 28743.0
-42.8868 28743.0
-41.2346 28743.0
-39.5846 28743.0
-45.3804 28744.0
-43.7284 28744.0
-42.0808 28744.0
-40.4308 28744.0
-38.7792 28744.0
-45.4610 28745.0
-43.8098 28745.0
-42.1594 28745.0
-40.5126 28745.0
-38.8608 28745.0
-45.1552 28746.0
-43.5100 28746.0
-41.8678 28746.0
-40.2296 28746.0
-38.5952 28746.0
-44.9840 28747.0
-43.3334 28747.0
-40.0314 28747.0
-38.3800 28747.0
-45.8540 28748.0
-44.2030 28748.0
-42.5522 28748.0
-40.9032 28748.0
-39.2532 28748.0
-45.5312 28749.0
-43.9034 28749.0
-42.2568 28749.0
-40.6044 28749.0
-38.9540 28749.0
-44.8084 28750.0
-43.1594 28750.0
-41.5118 28750.0
-39.8642 28750.0
-38.2144 28750.0
-44.8434 28751.0
-43.1930 28751.0
-41.5444 28751.0
-39.8958 28751.0
-38.2454 28751.0
-44.8526 28752.0
-43.2026 28752.0
-41.5494 28752.0
-39.8990 28752.0
-38.2494 28752.0
-45.4156 28753.0
-43.7656 28753.0
-42.1140 28753.0
-40.4638 28753.0
-38.8128 28753.0
Re: Locating desired values in an array. [message #89094 is a reply to message #89093] Wed, 23 July 2014 23:26 Go to previous messageGo to next message
Moritz Fischer is currently offline  Moritz Fischer
Messages: 32
Registered: June 2013
Member
Unless you know more about the nature of those track numbers I'd loop
over them. You can get the track numbers using UNIQ() and then do
something like this:

tn = uniq( yourarray[1,*] )
value = fltarr( n_elements( tn ) )

for i=0,n_elements(tn)-1 do begin

subset = WHERE( yourarray[1,*] EQ tn[i] )

void = min(abs( yourarray[0,subset] + 42. ), min_index )

value[i] = yourarr[0, subset[min_index] ]

end


Am 24.07.2014 03:50, schrieb gpeterso@ucsc.edu:
> So i have an array that is [2,58]. I need the number that is closet
> to -42 for each time the track number increases by one. So for
> example for track number 28742 I should get the third bin number of
> -42.6996. I am very confused on how to do approach this.
>
>
>
Re: Locating desired values in an array. [message #89098 is a reply to message #89093] Thu, 24 July 2014 05:13 Go to previous messageGo to next message
Matthew Argall is currently offline  Matthew Argall
Messages: 286
Registered: October 2011
Senior Member
Here is a solution using Histogram and reverse indices.

;Create data
tn = [replicate(3, 5), replicate(2, 3), replicate(9, 8)]
lat = 42 + randomu(5, n_elements(tn))

;Histogram to find repeated track numbers
hist = histogram(tn, MIN=0, REVERSE_INDICES=ri)

;Allocate memory to result.
nHist = n_elements(hist)
theNumber = fltarr(nHist)
theIndex = intarr(nHist) - 1

;Loop through each bin
for i = 0, nHist-1 do begin
;Only analyze bins with something in them
if ri[i+1] gt ri[i] then begin
;Find the closest number and its index
theNumber[i] = min( abs( 42 - abs(lat[ri[ri[i]:ri[i+1]-1]]) ), iMax )
theIndex[i] = ri[i] + iMax
endif
endfor

;Weed out empty histogram bins
iKeep = where(theIndex ne -1)
theNumber = theNumber[iKeep]
theIndex = theIndex[iKeep]
Re: Locating desired values in an array. [message #89104 is a reply to message #89094] Thu, 24 July 2014 11:19 Go to previous messageGo to next message
gpeterso%ucsc.edu is currently offline  gpeterso%ucsc.edu
Messages: 2
Registered: July 2014
Junior Member
When I try your way I get an error that says:
Attempt to subscript yourarray with subset is out if range
Re: Locating desired values in an array. [message #89112 is a reply to message #89104] Fri, 25 July 2014 01:16 Go to previous message
Moritz Fischer is currently offline  Moritz Fischer
Messages: 32
Registered: June 2013
Member
Sorry, my bad. UNIQ returns indices, not values. So it's even simpler:

idx = [-1,uniq( yourarray[1,*] )]
value = fltarr( n_elements(idx)-1 )

for i=0,n_elements(idx)-2 do begin

void = min(abs( yourarray[0, idx[i]+1:idx[i+1]] + 42. ), min_index )

value[i] = yourarray[0, idx[i]+1+min_index ]

end

However, note that this only works if the track numbers are sorted!
I'd stick to Matthew's approach, which is more generic!


Am 24.07.2014 20:19, schrieb gpeterso%ucsc.edu@gtempaccount.com:
> When I try your way I get an error that says: Attempt to subscript
> yourarray with subset is out if range
>
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: IDL and C inter-language communication / call_external()
Next Topic: how can you convert CMIP5 model output time ?

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Wed Oct 08 13:48:06 PDT 2025

Total time taken to generate the page: 0.00660 seconds