You want "closest" length and width but do not explain how YOU define "closest". For example if you have length & width of a rectangle 2 ways of comparing them could be perimeter or area or something like delta length + delta width or you may want to square those differences (like variances) so (delta length)^2 + (delta width)^2 or something else all together. While you are at it, you should consider 'tie breakers' so if the equation you use ends up with a tie do you just use either one or should you consider an alternative value (or values) to break the tie.