Finding the closest value from a matrix

M

Mahadev Ittina

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
 
M

Markus Roberts

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

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
 
M

Mahadev Ittina

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"
 
M

Mahadev Ittina

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.
 
S

Seebs

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
 
P

Phrogz

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.
 
M

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 = [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}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top