3

Say I have two arrays :

a=[10 21 50 70 100 120];
b=[18 91];

I want to match the (single) element across a and b that are closest AND within 10 units away.

Result :

idxa=[1 2 3 4 5 6]

idxb=[2 5]

where the matching elements share the same number.

I am confused because I am unsure how to ensure (for example) that 18 matches with 21 instead of 10 because they both meet the requirements of being within 10 units of each other. Also, I'd like to do this across several (up to 8) lists and the code is becoming overly complicated and I feel like there is an easy solution that I'm missing. I'm not worried about efficiency because the lengths of the lists are small.

Thank you!

Ahmad Azwar Anas
  • 1,289
  • 13
  • 22
stuppie
  • 384
  • 1
  • 8
  • 18
  • If you do this across multiple lists, are you always comparing to the same one? Or do you want to find the items "common" to all three? Also: What happens in case of ties? – Jonas Feb 23 '13 at 21:19
  • I want to find items "common" to all three. The real data are actually decimals, so there shouldn't be ties. If there are, it should just take whichever it finds first.. – stuppie Feb 23 '13 at 22:42
  • This looks to me much like a minimum cost maximum bipartite matching problem. It's also known as [Assignment problem](http://en.wikipedia.org/wiki/Assignment_problem) – Juan Lopes Feb 24 '13 at 01:27

2 Answers2

0

Your arrays seem to be sorted (I am going to proceed on that assumption; if not, you could simply sort them).

Have you tried merging the multiple arrays into one larger array? (Similar to the merge step of a merge sort). This would be a good starting point as it would reduce your problem to 'Find closet element in an array', which is trivial in comparison.

This would also allow you to remove duplicates; ie reduce all '21's to a single '21' in the array.

To ensure that 18 matches 21 instead of 10, you would want to calculate the difference between your key (18) and each value within 10 units ([10,21)), and then choose the one with the lowest difference.

UPDATE: In response to your comment about only find values common to all arrays, this could be done when merging the arrays by finding the intersection of the arrays, which may actually a predefined method depending on your language.

Nick Mitchinson
  • 5,452
  • 1
  • 25
  • 31
0

For small arrays this can be done by brute force:

(1) Iterate the smaller of the two arrays, then the larger array
(2) Keep track of "The Closest Match So Far" CMSF
(3) If you find a better match, update CMSF
(4) When you reach the end of the list, if the CMSF is <= 10 keep it, otherwise ignore this item (it has no match)

Tyler Durden
  • 11,156
  • 9
  • 64
  • 126
  • If the arrays are sorted (which they seem to be) this doesn't have to iterate the full array. There is no need to track the CMSF if it is farther than the threshold (in this case 10), and there is no need to continue after a CMSF < 10 is set and an element with different greater than 10 is found (again, assuming the arrays are sorted) – Nick Mitchinson Feb 23 '13 at 23:53