1

For example I have:

array[3] arr1 = {.5,.5,0}
array[3] arr2 = {.5,0,.5}
array[3] arr3 = {0,.5,.5}
array[3] findarr = {1,1,0}

Obviously, arr1 is the best match for findarr. I already have a simple algorithm working but I want to know if there is a formula for how to do this?

Thanks!

user488792
  • 1,943
  • 7
  • 31
  • 38
  • So "best match" means closest in numeric value with order considered? Anyway, let's see your algorithm! – BoltClock Mar 02 '11 at 08:02

2 Answers2

2

f = pow(arrX[0]-findarr[0],2) + pow(arrX[1]-findarr[1],2) + pow(arrX[2]-findarr[2],2);

pow(x,2) == x*x

So, "the best match" will be array, with smallest f

CyberDem0n
  • 14,545
  • 1
  • 34
  • 24
  • hi thanks! thats what im using now but im not squaring. im going to add that – user488792 Mar 02 '11 at 08:31
  • @user488792: Depending on what you are doing, you might also want to try `fabs(arrX[0]-findarr[0])` instead of `pow(...,2)`, or take the maximum of the absolute values of the differences. These are different vector norms instead of Euclidean distance (the answer suggests using distance squared, which is equivalent for comparisons). – Jeremiah Willcock Mar 02 '11 at 08:35
1

Answered here already. Just go through it to get an idea.
You can do it in similar manner.

A bit related.

In PHP, you can do it using array_intersect function.[+]

Community
  • 1
  • 1
Saurabh Gokhale
  • 53,625
  • 36
  • 139
  • 164