From: "Shot (Piotr Szotkowski)" Date: 2009-11-22T06:30:47+09:00 Subject: Re: Finding the closest value from a matrix --ZoaI/ZTpAVc4A5k6 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Mahadev Ittina: > They are limited to those spacings and those diameters, as they > are industry standards. However there are 5 more higher spacings. That=E2=80=99s ok, then the matrix is small enough to be kept in memory (and you can pregenerate it and only search on runtime, of course): # pregenerate diameters =3D [8, 10, 12, 16] spacings =3D [85, 90, 100, 125, 150, 175] # extend as needed values =3D diameters.product(spacings).map { |d, s| [0.5 * Math::PI * d = ** 2 / s, d, s] }.sort # search values.find { |x,| x > 1.1 } # =3D> [1.11701072127637, 8, 90] values.find { |x,| x > 2 } # =3D> [2.26194671058465, 12, 100] > So basically you are creating a map? That's filled with the values? I=E2=80=99m mapping every diameter+spacing pair to [value, diameter, spacin= g] triple, then I=E2=80=99m sorting the triples and picking the first one with= the first element (the value) larger than the desired input. > Thats quiet interesting. I tried the exact same thing, it says > "undefined method 'product' for for [8, 10, 12, 16]:Array" The above works in Ruby 1.8.7 and 1.9.1. If you run an earlier Ruby 1.8 version, you can either use the backports gem=C2=B9 or implement a simple variation tailored to your case: class Array def product other inject([]) do |result, my| result.concat other.map { |their| [my, their] } end end unless method_defined? :product end [:a, :b].product [:c, :d] # =3D> [[:a, :c], [:a, :d], [:b, :c], [:b, :d]] =C2=B9 http://github.com/marcandre/backports =E2=80=94 Shot --=20 Deleted code is debugged code. [Jeff Sickel] --ZoaI/ZTpAVc4A5k6 Content-Type: application/pgp-signature; name="signature.asc" Content-Description: Digital signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.9 (GNU/Linux) iEYEARECAAYFAksIW/kACgkQi/mCfdEo8UqgVACgoPTa9W4zKw7SwBDW9UBsb23o iLYAniIKuWZuIacO8HBMo7gSnc/JI9MS =/o7v -----END PGP SIGNATURE----- --ZoaI/ZTpAVc4A5k6--