Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Algorithm to compare number sets (Mind bender????)

Status
Not open for further replies.

TheInsider

Programmer
Jul 17, 2000
796
0
0
CA
Hi,
I have the following dilemma:

I want to compare sets of 2 or 3 numbers with each other to find the closest match. Lets say I want to compare 2 numbers for the following example. I have a multi-dimensional array X[5][2] containing the following arbitrary data:

X[0][0] = 1, X[0][1] = 2
X[1][0] = 2, X[1][1] = 4
X[2][0] = 5, X[2][1] = 8
X[3][0] = 7, X[3][1] = 9
X[4][0] = 3, X[4][1] = 1

Therefore my sets are:

(1, 2)
(2, 4)
(5, 8)
(7, 9)
(3, 1)

Now I want to find the index (pair) in the array that contains the closest match to the following arbitrary sets of numbers:

(4, 9)
(5, 6)
(9, 1)

My initial solution was to take the number pairs and turn them into numbers themselves.

For example the array sets would become:
(1, 2) -> 12
(2, 4) -> 24
(5, 8) -> 58
(7, 9) -> 79
(3, 1) -> 31

And my number sets to compare would become:

(4, 9) -> 49
(5, 6) -> 56
(9, 1) -> 91

Ok, now I would go through each number I created from the sets in the array and subtract my number from it. Keeping in mind to subtract the smaller number from the larger. The number created from the array set, that when subtracted from my number with the smallest difference, would be my closest match...right?

So:

49 best matches 58 -> 58 - 49 = 9 (smallest difference)
56 best matches 58 -> 58 - 56 = 2 (smallest difference)
91 best matches 79... wait a minute! The difference is being heavily weighted on the "9" in (9, 1). Neither of these two numbers should take precedence so (3, 1) is a better match, but my system does not pick this up! I would also like to be able to do this with sets of 3 or 4 numbers. Any mathematicians out there who can give me a better algorithm? Obviously creating numbers out of the set and subtracting them is not the way to go.

Any help is GREATLY appreciated!!!!! Thanks in advance.

Rob Marriott
rob@career-connections.net
 
Hi,
By closest, I do mean the smallest difference. I guess the best analogy of what I am doing would be to think of these sets as plotted points on a grid. I plot five points. Now I want to take a single arbitrary point and find the closest of the five points. To make things slightly harder, I REALLY want to use sets of 3, not 2. ie. (x, y, z)
I am using an array of structures. The structure has one variable for x, one for y, and one for z. Then I have a single-dimension array that holds the individual structures or "points". I want to write a function that receives as parameters the arbitrary x, y, and z points and returns the index of the single-dimension array containing the closest point.
Thanks,

Rob Marriott
rob@career-connections.net
 
Thankyou all for looking at my thread but I think I found the solution already.

Rob Marriott
rob@career-connections.net
 
You could also treat the sets of numbers as Points on a graph and then use the distance formula to examine the distance between the "points".
Distance Formula: Distance=SQRT(SQR(X1-X2)+SQR(Y1-Y2)+SQR(Y1-Y2)) For 3D points.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top