If you have a problem or need to report a bug please email : support@dsprobotics.com
There are 3 sections to this support area:
DOWNLOADS: access to product manuals, support files and drivers
HELP & INFORMATION: tutorials and example files for learning or finding pre-made modules for your projects
USER FORUMS: meet with other users and exchange ideas, you can also get help and assistance here
NEW REGISTRATIONS - please contact us if you wish to register on the forum
Users are reminded of the forum rules they sign up to which prohibits any activity that violates any laws including posting material covered by copyright
comparing arrays in ruby (problem)
14 posts
• Page 1 of 2 • 1, 2
comparing arrays in ruby (problem)
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.
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 559 times
Need to take a break? I have something right for you.
Feel free to donate. Thank you for your contribution.
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)
Here's my attempt:
Hoping one of the Ruby Gurus will be along shortly to show me how it should be done
Hoping one of the Ruby Gurus will be along shortly to show me how it should be done
-
DaveyBoy - Posts: 131
- Joined: Wed May 11, 2016 9:18 pm
- Location: Leeds UK
Re: comparing arrays in ruby (problem)
- 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)
- tulamide
- Posts: 2714
- Joined: Sat Jun 21, 2014 2:48 pm
- Location: Germany
Re: comparing arrays in ruby (problem)
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.
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 566 times
Need to take a break? I have something right for you.
Feel free to donate. Thank you for your contribution.
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)
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"
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.
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)
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.
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)
- tulamide
- Posts: 2714
- Joined: Sat Jun 21, 2014 2:48 pm
- Location: Germany
Re: comparing arrays in ruby (problem)
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, I'm curious why you are pushing to array rather than mapping, is it more efficient? faster?
Thanks in advance
-
DaveyBoy - Posts: 131
- Joined: Wed May 11, 2016 9:18 pm
- Location: Leeds UK
Re: comparing arrays in ruby (problem)
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)
- tulamide
- Posts: 2714
- Joined: Sat Jun 21, 2014 2:48 pm
- Location: Germany
Re: comparing arrays in ruby (problem)
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.
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.
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)
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
-
DaveyBoy - Posts: 131
- Joined: Wed May 11, 2016 9:18 pm
- Location: Leeds UK
14 posts
• Page 1 of 2 • 1, 2
Who is online
Users browsing this forum: No registered users and 83 guests