Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Finding the closest value from a matrix

3 views
Skip to first unread message

Mahadev Ittina

unread,
Nov 21, 2009, 3:04:47 PM11/21/09
to
I have a matrix that looks something like this


85 90 100 125 150 175
8 1.183 1.118 1.006 0.805 0.671 0.575
10 1.847 1.744 1.57 1.256 1.047 0.897
12 2.659 2.511 .. .. ... ...
16 ... ... .. ... ... ..

Say, I have a value of 1.1, I want it to automatically choose the
closest bigger value i.e. 1.118 and say 8 mm diameter at 90 mm spacing.
How can this be implemented? I thought of creating hashes. but it seems
too complicated. If anyone is wondering about the formula it goes like
this.

value = 2 x area of circle / spacing
--
Posted via http://www.ruby-forum.com/.

Markus Roberts

unread,
Nov 21, 2009, 3:48:04 PM11/21/09
to

This sounds an awful lot like homework.

If there is a finite list of spacings, you can figure out the
ideal diameter for each one (by reversing the formula), round each up to
the closest permited diamiter, and choose the one cooresponding to
the smallest computed value (using the given formula). Look at
Array#collect, Array#find, Array#sort, and Array#first.

-- MarkusQ


Mahadev Ittina

unread,
Nov 21, 2009, 3:49:41 PM11/21/09
to
Hello Thanks. They are limited to those spacings and those diameters, as
they are industry standards. However there are 5 more higher spacings.
So basically you are creating a map? That's filled with the values?
Thats quiet interesting. I tried the exact same thing, it says
"undefined method 'product' for for [8, 10, 12, 16]:Array"

Mahadev Ittina

unread,
Nov 21, 2009, 3:52:39 PM11/21/09
to
>
> This sounds an awful lot like homework.
>
> If there is a finite list of spacings, you can figure out the
> ideal diameter for each one (by reversing the formula), round each up to
> the closest permited diamiter, and choose the one cooresponding to
> the smallest computed value (using the given formula). Look at
> Array#collect, Array#find, Array#sort, and Array#first.
>
> -- MarkusQ

I assure you, this is no homework. I am complete newbie, and I am
creating a plug-in to work with SketchUp. I could email it you if you
would like to see.

Seebs

unread,
Nov 21, 2009, 5:24:59 PM11/21/09
to
On 2009-11-21, Mahadev Ittina <mit...@gmail.com> wrote:
> Hello Thanks. They are limited to those spacings and those diameters, as
> they are industry standards. However there are 5 more higher spacings.
> So basically you are creating a map? That's filled with the values?
> Thats quiet interesting. I tried the exact same thing, it says
> "undefined method 'product' for for [8, 10, 12, 16]:Array"

There's a few options.

The simplest is just to iterate through the whole thing, remembering:
* The location of the closest larger value you've previously seen
* How close that value was

For each value, if it is larger, see how close it is; if it's closer than
your previous best guess, it becomes your new best guess. At the end of
the process, you have the best guess.

-s
--
Copyright 2009, all wrongs reversed. Peter Seebach / usenet...@seebs.net
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!

Phrogz

unread,
Nov 22, 2009, 9:42:04 AM11/22/09
to
On Nov 21, 1:04 pm, Mahadev Ittina <mitt...@gmail.com> wrote:
> I have a matrix that looks something like this
>
>        85        90       100       125        150         175
> 8     1.183    1.118     1.006     0.805      0.671      0.575
> 10    1.847    1.744     1.57      1.256      1.047      0.897
> 12    2.659    2.511      ..        ..           ...        ...
> 16    ...       ...        ..     ...           ...        ..
>
> Say, I have a value of 1.1, I want it to automatically choose the
> closest bigger value i.e. 1.118 and say 8 mm diameter at 90 mm spacing.
> How can this be implemented? I thought of creating hashes. but it seems
> too complicated. If anyone is wondering about the formula it goes like
> this.

Create a one-dimensional array where each entry records the value, row
and column number (or row and column headers).

# This should be programmatically done by iterating your array
# not explicitly as I'm showing it here.
Descriptor = Struct.new :row, :col, :value
a = [
Descriptor.new(0, 0, 1.183),
Descriptor.new(0, 1, 1.118),
Descriptor.new(0, 1, 1.006),
# ..etc.
]

# Sort the array by value
a = a.sort_by{ |desc| desc.value }

# Create a method that finds the closest (larger) value in the array
# via a binary search: http://en.wikipedia.org/wiki/Binary_search
# (Left as an exercise for the reader.)

# Now you know what row and column it came from.

Mahadev Ittina

unread,
Nov 22, 2009, 3:44:54 PM11/22/09
to
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 = [6, 8, 10, 12, 16, 20, 25, 32, 40]
number = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
diameters = [8, 10, 12, 16]
spacings = [85, 90, 100, 125, 150, 175, 200, 225, 250, 275, 300]
@shear_links = diameters.product(spacings).map { |d, s| [0.5 * Math::PI
* d ** 2 / s, d, s] }.sort.find { |x,| x > @asw_s }
@compression = reinforcement.product(number).map { |r, n| [0.25 *
Math::PI * r ** 2 * n, r, n] }.sort.find { |x,|x > @comp_steel}
@tension = reinforcement.product(number).map { |r, n| [0.25 * Math::PI *
r ** 2 * n, r, n] }.sort.find { |x,|x > @ten_steel}

0 new messages