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

**URL:** <https://discourse.julialang.org/t/how-to-search-an-array-for-the-index-coresponding-to-the-nearest-value/83190>\
**Category:** General Usage\
**Tags:** question\
**Created:** [June 22, 2022, 3:09pm UTC](https://discourse.julialang.org/t/how-to-search-an-array-for-the-index-coresponding-to-the-nearest-value/83190 "2022-06-22T15:09:36Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![TLTYNS](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tltyns/32/37361_2.png) [@TLTYNS](https://discourse.julialang.org/u/TLTYNS)\
**Post date:** [June 22, 2022, 3:09pm UTC](https://discourse.julialang.org/t/how-to-search-an-array-for-the-index-coresponding-to-the-nearest-value/83190/1 "2022-06-22T15:09:36Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![lawless-m](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lawless-m/32/30869_2.png) [@lawless-m](https://discourse.julialang.org/u/lawless-m)\
**Post date:** [June 22, 2022, 3:36pm UTC](https://discourse.julialang.org/t/how-to-search-an-array-for-the-index-coresponding-to-the-nearest-value/83190/2 "2022-06-22T15:36:10Z")

</div>

This does what I think you want

```julia
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
julia> b[partialsortperm(abs.(b .- t), 1:5)]
5-element Vector{Float64}:
 0.5266419173203005
 0.5280409414251348
 0.5296562291180309
 0.5347218563730891
 0.5401489751301409

```

---

<div class="post-metadata">

**Author:** ![halleysfifthinc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/halleysfifthinc/32/206280_2.png) [@halleysfifthinc](https://discourse.julialang.org/u/halleysfifthinc)\
**Post date:** [June 22, 2022, 3:42pm UTC](https://discourse.julialang.org/t/how-to-search-an-array-for-the-index-coresponding-to-the-nearest-value/83190/3 "2022-06-22T15:42:11Z")

</div>

The NearestNeighbors package can do this:

> **[GitHub - KristofferC/NearestNeighbors.jl: High performance nearest neighbor...](https://github.com/KristofferC/NearestNeighbors.jl)**
>
> High performance nearest neighbor data structures and algorithms for Julia. - GitHub - KristofferC/NearestNeighbors.jl: High performance nearest neighbor data structures and algorithms for Julia.

Something like

```julia-repl
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);

```

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [June 22, 2022, 3:46pm UTC](https://discourse.julialang.org/t/how-to-search-an-array-for-the-index-coresponding-to-the-nearest-value/83190/4 "2022-06-22T15:46:53Z")

</div>

> [@lawless-m](#):
>
> `partialsortperm(abs.(a .- t), 1)`

Faster, and no allocations:

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

```

For more general distances there is

```julia
using LinearAlgebra: norm
findmin(x->norm(x-t), a)

```

---

<div class="post-metadata">

**Author:** ![lawless-m](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lawless-m/32/30869_2.png) [@lawless-m](https://discourse.julialang.org/u/lawless-m)\
**Post date:** [June 22, 2022, 4:01pm UTC](https://discourse.julialang.org/t/how-to-search-an-array-for-the-index-coresponding-to-the-nearest-value/83190/5 "2022-06-22T16:01:34Z")

</div>

Just to note: `findmin` requires Julia 1.7 or later.
