0

Suppose I have 3 numpy arrays. It could be more than 3 arrays, however.

import numpy as np
INTERVAL = 2
array1 = np.array([1,5,10,15,20,25,30])
array2 = np.array([1,10,50,100,150,200,250,300])
array3 = np.array([3,8,12])

For a given set of elements to match in the above arrays, each element must be within INTERVAL of each other. The actual index position of the elements in the array do not matter in the comparison. Order is not guaranteed. It is about any element at any position in the arrays are each within INTERVAL of each other.

Example matches that would be returned from the above 3 arrays:

Example#1
array1 : 1
array2 : 1
array3 : 3

Example#2
array1 : 10
array2 : 10
array3 : 8

Example#3
array1 : 10
array2 : 10
array3 : 12

Bonus points :

In the event where there could be multiple matches with the same elements, only return the one with the lowest sum. For example, Example#2 and Example#3 share elements, but Example#2 & Example#1 should be returned and not Example#3

Any suggestions on how I should go about this?

BrokenBenchmark
  • 18,126
  • 7
  • 21
  • 33
geekygeek
  • 611
  • 4
  • 15

1 Answers1

0

You can create a Cartesian product using np.meshgrid (credit here for the Cartesian product code). Then, you can use np.where to extract combinations that meet the difference constraint:

product = np.array(np.meshgrid(array1, array2, array3)).T.reshape(-1,3)
result = product[np.where(product.max(axis=1) - product.min(axis=1) <= INTERVAL)]

repr(result)

This outputs:

array([[ 1,  1,  3],
       [10, 10,  8],
       [10, 10, 12]])
BrokenBenchmark
  • 18,126
  • 7
  • 21
  • 33