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))
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
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?
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
.
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).
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
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
.
Ah yes! Good catch! I was confused and meant to say "recitlinear"... So:
searchsortedXXXX
Last updated: Nov 06 2024 at 04:40 UTC