Stream: helpdesk (published)

Topic: finding index of numbers mapped to intervals


view this post on Zulip Gerhard Dorn (Mar 30 2022 at 09:47):

I have a vector (or an array) of random numbers
x = [0.2, 3.4, 1.2, 3, 2.2]
and would like to find the position of those numbers (get a Cartesian Index)
for a given regular grid / range of intervals
y = -1:0.5:4
so to get the index set
ix = [3, 9, 5, 9, 7]
with y[ix[j]] <= x[j] < y[ix[j]+1]
(e.g. for j = 1: 0 <= 0.2 < 0.5)
Is there an efficient build-in function in Julia or StatsBase for this mapping to intervals?

(I know to use
Int(ceil((x .- y[1]) ./ (y[2] - y[1])))
but I would like to get CartesianCoordinates for a 3d grid
(for the irregular spacing case: I guess some sorting would help))

view this post on Zulip Sundar R (Mar 31 2022 at 14:37):

For the 1D case, you can use searchsortedlast as mentioned in this SO answer. Here the usage would be something like

julia> searchsortedlast.(Ref(y), x)
5-element Vector{Int64}:
 3
 9
 5
 9
 7

view this post on Zulip Sundar R (Mar 31 2022 at 14:39):

I'm not sure I understand the 3D grid part though. What do x and y actually look like in that case, as a MWE?

view this post on Zulip Benoit Pasquier (Mar 31 2022 at 22:46):

I often have to do similar things and depending on the case I used either searrchsortedfirst/searchsortedlast, Interpolations.jl, or NearestNeighbours.jl. In the 3D case when I can't use the built-ins, I interpolate/nearestneighbour-search the LinearIndex.

view this post on Zulip Benoit Pasquier (Mar 31 2022 at 22:48):

For me the fastest is if you can get away with searchsorted, and the most accurate is to use nearestneighbors (because then you can use any distance, which is useful, e.g., for lat,lon,altitude coordinates on the earth).

view this post on Zulip Benoit Pasquier (Mar 31 2022 at 22:52):

Importantly if your grid is regular and rectangular — that is if you can index into it with (i,j,k)— and if you can deal with each dimension 1 by 1, then you should be able to just use searchsorted on each dimension to get a fast answer

view this post on Zulip Gunnar Farnebäck (Apr 01 2022 at 09:43):

For a regular grid, directly computing the index from the coordinate is much faster than searchsorted. For 3D, do this coordinate by coordinate, possibly with broadcasting, and wrap the indices into a CartesianIndex.

view this post on Zulip Benoit Pasquier (Apr 04 2022 at 03:52):

Ah yes! Good catch! I was confused and meant to say "recitlinear"... So:

view this post on Zulip Benoit Pasquier (Apr 04 2022 at 03:52):

view this post on Zulip Benoit Pasquier (Apr 04 2022 at 03:53):


Last updated: Oct 02 2023 at 04:34 UTC