How to search an array for the index coresponding to the nearest value?

I have two separate data sets and each of them have a different time axis (distance between values). I want to write a function where I provide a time(float64) and it tells me the closest index value in both arrays. I cannot use the findall() tool though since my data sets are float64 type datasets. Is there a tool similar to findall() that I could use to help me?

This does what I think you want

julia> a = rand(100);

julia> b = rand(100);

julia> t = 0.5;

julia> partialsortperm(abs.(a .- t), 1)
13

julia> a[13]
0.4932697998859428

julia> partialsortperm(abs.(b .- t), 1)
74

julia> b[74]
0.5266419173203005

partiaslsortperm returns the given index or range (1 in this case) of the sorted version of its arguments

julia> b[partialsortperm(abs.(b .- t), 1:5)]
5-element Vector{Float64}:
 0.5266419173203005
 0.5280409414251348
 0.5296562291180309
 0.5347218563730891
 0.5401489751301409

The NearestNeighbors package can do this:

Something like

julia> using NearestNeighbors

julia> time1 = collect(0:0.01:10)';

julia> time2 = time1 .+ randn(Float32, size(time1))./100;

julia> tree = KDTree(time2)
KDTree{StaticArrays.SVector{1, Float64}, Euclidean, Float64}
  Number of points: 1001
  Dimensions: 1
  Metric: Euclidean(0.0)
  Reordered: true

julia> idxs, = nn(tree, time1);

Faster, and no allocations:

findmin(x->abs(x-t), a)

For more general distances there is

using LinearAlgebra: norm
findmin(x->norm(x-t), a)
2 Likes

Just to note: findmin requires Julia 1.7 or later.