Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

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

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

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

 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?


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

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

 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.

Rob Marriott

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

Thankyou all for looking at my thread but I think I found the solution already.

Rob Marriott

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close