Re: nearest number [message #49605] |
Mon, 07 August 2006 09:24 |
James Kuyper
Messages: 425 Registered: March 2000
|
Senior Member |
|
|
kuyper@wizard.net wrote:
> bressert@gmail.com wrote:
>> I thought about the problem more and realized a simple solution:
>>
>> smallest = min(abs(array - floating_value))
>> index = where((array - floating_value) eq smallest, count)
>
> Almost: that should be "abs(array-floating_value) eq smallest".
>
> I'd be inclined to pull out the common code, and write it as
>
> diff = abs(array-floating_value)
> index = where(array eq min(array))
Agh! That should, of course, have been
index = where(diff eq min(diff))
|
|
|
Re: nearest number [message #49608 is a reply to message #49605] |
Mon, 07 August 2006 08:09  |
mchinand
Messages: 66 Registered: September 1996
|
Member |
|
|
In article <1154956439.920334.266030@m73g2000cwd.googlegroups.com>,
Timm Weitkamp <timm.weitkamp@iss.fzk.de> wrote:
> kuyper@wizard.net wrote:
>> bressert@gmail.com wrote:
>>> I thought about the problem more and realized a simple solution:
>>>
>>> smallest = min(abs(array - floating_value))
>>> index = where((array - floating_value) eq smallest, count)
>>
>> Almost: that should be "abs(array-floating_value) eq smallest".
>>
>> I'd be inclined to pull out the common code, and write it as
>>
>> diff = abs(array-floating_value)
>> index = where(array eq min(array))
>
> Or simply,
> void = min(abs(array - floating_value), index)
>
I was going to post the same thing last night, but this optional argument to MIN will only return
the index of the first element that is the minimum of the array even if there are multiple
occurrences of the minimum value. Which may be alright, depending on what the original poster
wants.
--Mike
--
Michael Chinander
m-chinander@uchicago.edu
Department of Radiology
University of Chicago
|
|
|
Re: nearest number [message #49610 is a reply to message #49608] |
Mon, 07 August 2006 06:52  |
btt
Messages: 345 Registered: December 2000
|
Senior Member |
|
|
bressert@gmail.com wrote:
> Hello,
>
> Im writing a bit of IDL code where a large array of floating numbers is
> compared to one floating number. The aim of comparing the array to the
> floating number to find which the values "closest" match. I had the
> idea that I could write an algorithm that starts with the following
>
> ===========
> index = where(array eq floating_value, count)
>
> if (size(index, /n_elements) eq 1) AND (index eq -1) then begin ...
> ===========
>
>
> This bit would find the index numbers of the array that would be
> exactly the same to the floating number. If there is no number the
> where function returns a -1. From this point, I could try and round off
> the floating number and numbers in the array with smaller decimal
> places until there is a match. Would this be the best approach? Or is
> there already a built code in IDL or from someone else that does this?
> Any help is appreciated. Thanks!
Hi,
Check out the following thread - JD Smith discusses a couple of
approaches to solve this kind of problem.
http://tinyurl.com/e6ke7
Cheers,
Ben
|
|
|
Re: nearest number [message #49611 is a reply to message #49610] |
Mon, 07 August 2006 06:18  |
Benjamin Hornberger
Messages: 258 Registered: March 2004
|
Senior Member |
|
|
bressert@gmail.com wrote:
> Hello,
>
> Im writing a bit of IDL code where a large array of floating numbers is
> compared to one floating number. The aim of comparing the array to the
> floating number to find which the values "closest" match.
IDL provides the VALUE_LOCATE function for this purpose. The array must
be monotonic, but of course you can sort it if necessary.
Benjamin
|
|
|
Re: nearest number [message #49612 is a reply to message #49611] |
Mon, 07 August 2006 06:13  |
Timm Weitkamp
Messages: 66 Registered: August 2002
|
Member |
|
|
kuyper@wizard.net wrote:
> bressert@gmail.com wrote:
>> I thought about the problem more and realized a simple solution:
>>
>> smallest = min(abs(array - floating_value))
>> index = where((array - floating_value) eq smallest, count)
>
> Almost: that should be "abs(array-floating_value) eq smallest".
>
> I'd be inclined to pull out the common code, and write it as
>
> diff = abs(array-floating_value)
> index = where(array eq min(array))
Or simply,
void = min(abs(array - floating_value), index)
Timm
|
|
|
Re: nearest number [message #49615 is a reply to message #49612] |
Sun, 06 August 2006 12:34  |
James Kuyper
Messages: 425 Registered: March 2000
|
Senior Member |
|
|
bressert@gmail.com wrote:
> I thought about the problem more and realized a simple solution:
>
> smallest = min(abs(array - floating_value))
> index = where((array - floating_value) eq smallest, count)
Almost: that should be "abs(array-floating_value) eq smallest".
I'd be inclined to pull out the common code, and write it as
diff = abs(array-floating_value)
index = where(array eq min(array))
|
|
|
Re: nearest number [message #49616 is a reply to message #49615] |
Sun, 06 August 2006 09:58  |
bressert@gmail.com
Messages: 8 Registered: February 2006
|
Junior Member |
|
|
I thought about the problem more and realized a simple solution:
smallest = min(abs(array - floating_value))
index = where((array - floating_value) eq smallest, count)
That should work for me without any problems I think
Cheers,
Eli
bressert@gmail.com wrote:
|
|
|