1

I have a numpy array of shape n x d. Each row represents a point in R^d. I want to filter this array to only rows within a given distance on each axis of a single point--a d-dimensional hypercube, as it were.

In 1 dimension, this could be:

array[np.which(array < lmax and array > lmin)]

where lmax and lmin are the max and min relevant to the point+-distance. But I want to do this in d dimensions. d is not fixed, so hard-coding it out doesn't work. I checked to see if the above works where lmax and lmin are d-length vectors, but it just flattens the array.

I know I could plug the matrix and the point into a distance calculator like scipy.spatial.distance and get some sort of distance metric, but that's likely slower than some simple filtering (if it exists) would be.

The fact I have to do this calculation potentially millions of times means Ideally I'd like a fast solution.

Faydey
  • 725
  • 1
  • 5
  • 12

1 Answers1

0

You can try this.

def test(array):
    large = array > lmin
    small = array < lmax
    return array[[i for i in range(array.shape[0])
        if np.all(large[i]) and np.all(small[i])]]

For every i, array[i] is a vector. All the elements of a vector should be in range [lmin, lmax], and this process of calculation can be vectorized.

xiao-Tiger
  • 46
  • 1
  • 3