comparing arrays in ruby (problem)

For general discussion related FlowStone
tester
Posts: 1786
Joined: Wed Jan 18, 2012 10:52 pm
Location: Poland, internet

comparing arrays in ruby (problem)

Post by tester »

From what I see, I need a solution to compare arrays (some clean compact code) in ruby in a way shown in the schematic.

Basically, there is an input array and reference table.
Each input value shoould be compared with reference table, and on output I'd like to get the index of the reference value, that meets desired criteria (i.e. index of a closest lowest value).
Thus, on output, there would be indexes of closest lower values from the reference table.

In second module, I'd like to extract the reference values at given indexes.

Thanks in advance.

This is a part of a larger schematic, which I will post after it's finished.
Attachments
comparator.fsm
(776 Bytes) Downloaded 789 times
Need to take a break? I have something right for you.
Feel free to donate. Thank you for your contribution.
User avatar
DaveyBoy
Posts: 131
Joined: Wed May 11, 2016 9:18 pm
Location: Leeds UK

Re: comparing arrays in ruby (problem)

Post by DaveyBoy »

Here's my attempt:

Nearest Lowest v3.06.fsm
(526 Bytes) Downloaded 785 times


Hoping one of the Ruby Gurus will be along shortly to show me how it should be done :D
tulamide
Posts: 2714
Joined: Sat Jun 21, 2014 2:48 pm
Location: Germany

Re: comparing arrays in ruby (problem)

Post by tulamide »

Code: Select all

a = [100, 200, 350, 400, 420, 500] ##reference
b = [75, 160, 348, 350, 409] ##values
c = [] ##result

b.each do |item|
  ##find minimum distance (=closest value) and add it to result
  c << a.min { |a,b| (a - item).abs <=> (b - item).abs }
end

c #== [100, 200, 350, 350, 400]
"There lies the dog buried" (German saying translated literally)
tester
Posts: 1786
Joined: Wed Jan 18, 2012 10:52 pm
Location: Poland, internet

Re: comparing arrays in ruby (problem)

Post by tester »

Thank you.

Attaching equal-loudness autogain formulator. Inside you will find a simple table for equal-loudness curve, and frequency-to-gain converter with linear interpolation. Such table can be more dense, and detailed in areas of larger dynamics, but it depends on user what they want. Interpolation acts between data points, so no need for hard science.
Attachments
autogain-equi.fsm
(1.95 KiB) Downloaded 803 times
Need to take a break? I have something right for you.
Feel free to donate. Thank you for your contribution.
tester
Posts: 1786
Joined: Wed Jan 18, 2012 10:52 pm
Location: Poland, internet

Re: comparing arrays in ruby (problem)

Post by tester »

tulamide, how in your case get not values, but indexes of these values?

Also, the logic is broken. It should find only closest lower value, to get it's index. so for 120 and 190 it will be index id of "100"
Need to take a break? I have something right for you.
Feel free to donate. Thank you for your contribution.
tulamide
Posts: 2714
Joined: Sat Jun 21, 2014 2:48 pm
Location: Germany

Re: comparing arrays in ruby (problem)

Post by tulamide »

I see.

But why would you want the index and not the reference value? It also has the advantage that you get the real closest value. However, to get the index, see below. Careful, it obviously can't find, what doesn't exist. In that case it returns nil.

Code: Select all

a = [100, 200, 350, 400, 420, 500] ##reference
b = [75, 160, 348, 350, 409, 201] ##values
c = [] ##result

b.each do |item|
  ##find lowest index of reference that's less than value
  c << a.rindex { |ref| ref <= item }
end

c #== [nil, 0, 1, 2, 3, 1]
"There lies the dog buried" (German saying translated literally)
User avatar
DaveyBoy
Posts: 131
Joined: Wed May 11, 2016 9:18 pm
Location: Leeds UK

Re: comparing arrays in ruby (problem)

Post by DaveyBoy »

ahhh! rindex, I should have thought of that, though i've never used it before . . very useful.

Tulamide, I'm curious why you are pushing to array rather than mapping, is it more efficient? faster? :?

Thanks in advance :)
tulamide
Posts: 2714
Joined: Sat Jun 21, 2014 2:48 pm
Location: Germany

Re: comparing arrays in ruby (problem)

Post by tulamide »

DaveyBoy wrote:Tulamide, I'm curious why you are pushing to array rather than mapping, is it more efficient? faster? :?

I chose it just for the demonstration purpose here. By seperating the important bit, it (to my mind) becomes more obvious, that you can use this just as well outside of an enumerable, for example as a method.

Map, as far as I know, does the exact same thing. Creating an array and filling it with the results of the block. I don't think either one is particularly faster.
"There lies the dog buried" (German saying translated literally)
tester
Posts: 1786
Joined: Wed Jan 18, 2012 10:52 pm
Location: Poland, internet

Re: comparing arrays in ruby (problem)

Post by tester »

tulamide,

By getting index of a certain value (that meets specific criteria), you can easily build a matrix of reference indexes, and then you can get various different kind of values (correlated but from different pools) at thexe indexes. So this is more generalized and scalable approach.

By getting individual values through (more complex?) computation, it's less efficient, and you need to approach each value independently.

In case of this simple equal-loudness autogain compensator - getting single index, gives you a matrix of 4 idenxes, that operate on 2 correlated tables.

As for efficiency, there is no problem if the input value just changes into something else, but the issue get's more pronounced if the input changes live at some speed (each step = full computation), and blue scenery is extensively active.

*

As for second part of your question, look inside the interpolator.

Input - bottom_neighbour / top_neighbour - bottom_neighbour = output - bottom_neighbour2 / top_neighbour2 - bottom_neighbour2

So the output follows the input curve, and the curve doesn't need to have equally spreaded points in this case.

Reference table is always present, array of values to compare too, because it's hardwired.
Need to take a break? I have something right for you.
Feel free to donate. Thank you for your contribution.
User avatar
DaveyBoy
Posts: 131
Joined: Wed May 11, 2016 9:18 pm
Location: Leeds UK

Re: comparing arrays in ruby (problem)

Post by DaveyBoy »

tulamide wrote:Map, as far as I know, does the exact same thing.


Thanks T . . . I never stopped to think about how Ruby works behind the scenes :)
Post Reply