From: "Shot (Piotr Szotkowski)" Date: 2009-11-24T02:12:41+09:00 Subject: Re: Finding the closest value from a matrix --V0207lvV8h4k8FAm Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Mahadev Ittina: > thanks for all the methods. I never > realised there were so many ways to do it! > I decided to do the 1st method. I have repeat the method > a couple of more times than shown here.. Its a good way. > reinforcement =3D [6, 8, 10, 12, 16, 20, 25, 32, 40] > number =3D [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] > diameters =3D [8, 10, 12, 16] > spacings =3D [85, 90, 100, 125, 150, 175, 200, 225, 250, 275, 300] > @shear_links =3D diameters.product(spacings).map { |d, s| [0.5 * Math::PI= * d ** 2 / s, d, s] }.sort.find { |x,| x > @asw_s } > @compression =3D reinforcement.product(number).map { |r, n| [0.25 * Math= ::PI * r ** 2 * n, r, n] }.sort.find { |x,|x > @comp_steel} > @tension =3D reinforcement.product(number).map { |r, n| [0.25 * Math::PI = * r ** 2 * n, r, n] }.sort.find { |x,|x > @ten_steel} How about some DRY-ing up of the code (and not recomputing the arrays every time)? :) def values rows, cols, &formula rows.product(cols).map do |row, col| [formula.call(row, col), row, col] end.sort end reinforcement =3D [6, 8, 10, 12, 16, 20, 25, 32, 40] number =3D [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] diameters =3D [8, 10, 12, 16] spacings =3D [85, 90, 100, 125, 150, 175, 200, 225, 250, 275, 300] @ds ||=3D values(diameters, spacings) { |d, s| 0.5 * Math::PI * d ** 2 /= s } @rn ||=3D values(reinforcement, number) { |r, n| 0.25 * Math::PI * r ** 2 *= n } @shear_links =3D @ds.find { |x,| x > @asw_s } @compression =3D @rn.find { |x,| x > @comp_steel } @tension =3D @rn.find { |x,| x > @ten_steel } =E2=80=94 Shot --=20 Your lust for power doomed 700 men to a watery grave. Yes, you sank my battleship =E2=80=93 but at what cost to your soul? Now go to your room. [Joshua Green Allen] --V0207lvV8h4k8FAm 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) iEYEARECAAYFAksKwoMACgkQi/mCfdEo8UqNfQCfcrd0NLW8cCJK+websUv+T/kX vZUAniVh32FCM4lSJwq02ycd1BYKOx6q =4lJa -----END PGP SIGNATURE----- --V0207lvV8h4k8FAm--