-
Notifications
You must be signed in to change notification settings - Fork 0
/
closest.pro
43 lines (41 loc) · 867 Bytes
/
closest.pro
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
function closest, array, value
;+
; NAME:
; CLOSEST
;
; PURPOSE:
; Find the element of ARRAY that is the closest in value to VALUE
;
; CATEGORY:
; utilities
;
; CALLING SEQUENCE:
; index = CLOSEST(array,value)
;
; INPUTS:
; ARRAY = the array to search
; VALUE = the value we want to find the closest approach to
;
; OUTPUTS:
; INDEX = the index into ARRAY which is the element closest to VALUE
;
; OPTIONAL PARAMETERS:
; none
;
; COMMON BLOCKS:
; none.
; SIDE EFFECTS:
; none.
; MODIFICATION HISTORY:
; Written by: Trevor Harris, Physics Dept., University of Adelaide,
; July, 1990.
;
;-
if (n_elements(array) le 0) or (n_elements(value) le 0) then index=-1 $
else if (n_elements(array) eq 1) then index=0 $
else begin
abdiff = abs(array-value) ;form absolute difference
mindiff = min(abdiff,index) ;find smallest difference
endelse
return,index
end