Support

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

finding elements in range

For general discussion related FlowStone

finding elements in range

Postby tester » Mon May 19, 2014 11:04 pm

Either I'm just tired or tired even more. :mrgreen:

Okay, I'd like to do something in ruby (greenery is too slow for it), and I'm having deja vu, that I asked about it in the past, but can't find it now... So on example:

Inputs:
float (min range)
float (max range)
float array (values to compare with range)

outputs:
array (indexes of values that are in min/max range in relation to base value)

example:

if range is between 95 and 105
and array contains elements:
90
94
98
101
107
110

then on output I'd like to get a list of indexes of values, that fit the range; it would be like here:
2
3

*

How to make it in ruby?
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: finding elements in range

Postby tester » Mon May 19, 2014 11:28 pm

I found only something like this:

output (@in.map {|v| (@min..@max) === v ? v : nil }).compact

but it outputs values, and I need indexes where these values are located.
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: finding elements in range

Postby Nubeat7 » Mon May 19, 2014 11:35 pm

Code: Select all
def event i,v
   a = []   
   @f.each_index do |i|
     a << i if (@f[i] >= @min) && (@f[i] <= @max)
   end
   output 0,a
end

just iterate through the array and compare if value is in range, if yes push index in an array
User avatar
Nubeat7
 
Posts: 1347
Joined: Sat Apr 14, 2012 9:59 am
Location: Vienna

Re: finding elements in range

Postby tester » Tue May 20, 2014 7:01 am

Thank you.

One more question. How to combine these two formulas, to have on one output list of indexes and on second output list of values at these indexes?

Basically what I'm trying to do is something like this:
- on two array inputs you connect list of values and list of corresponding names
- on third and fourth input you connect ranges, to see which above values fit them
- and on two array outputs you have list of values and list of corresponding names - that fit these ranges.
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: finding elements in range

Postby Nubeat7 » Tue May 20, 2014 12:20 pm

then also push the value instead of the index in a second array..

should be something like this (not tested..)
Code: Select all
def event i,v
   a = []   
   @f.each_index do |i|
      if (@f[i] >= @min) && (@f[i] <= @max)
         a << i
         b << @stringnames[i]
      end
   end
   output 0,a
end
User avatar
Nubeat7
 
Posts: 1347
Joined: Sat Apr 14, 2012 9:59 am
Location: Vienna

Re: finding elements in range

Postby tester » Tue May 20, 2014 11:20 pm

Thanks, I think I solved this one a bit differently

Code: Select all
def event i,v
   a = []   
   @tones.each_index do |i|
     a << i if (@tones[i] >= @min) && (@tones[i] <= @max)
   end
   output 0,@names.values_at(*a)
   output 1,(@tones.map {|v| (@min..@max) === v ? v : nil }).compact
end


Seems to work I guess. Now just need to connect somewhere a trigger, to run the operation manually.
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: finding elements in range

Postby tester » Wed May 21, 2014 12:11 am

It's rough but it's working. Question: is there a way to optimize it?

Code: Select all
def event(in_id, val)
   a = []   
   @tones.each_index do |i|
     a << i if (@tones[i] >= @min) && (@tones[i] <= @max)
   end
   if in_id == "trigger"
   output 0,@names.values_at(*a)
   output 1,(@tones.map {|v| (@min..@max) === v ? v : nil }).compact
end
end
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: finding elements in range

Postby Tronic » Wed May 21, 2014 9:25 am

Code: Select all
min = 92
max = 100
f = [90,94,98,101,107,110]
a_idx_in_range = []
a_idx_out_range = []

f.each_index do |i|
if f[i].between?(min, max)
  a_idx_in_range << i
elsif f[i]
  a_idx_out_range << i
end
end

watch ' in range' ,a_idx_in_range
watch 'out range', a_idx_out_range
Tronic
 
Posts: 539
Joined: Wed Dec 21, 2011 12:59 pm

Re: finding elements in range

Postby tester » Wed May 21, 2014 12:37 pm

Hi Tronic! What I basically meant speaking on optimization - is to:
- make more "uniform" the combination of these three operations that are happening in module.
- I have no idea whether what I did - prevents ruby from calculating something even if nothing is sent to outputs (by trigger).

Basically what happens here is:

- one input receives array of names/descriptors
- second input receives array of values that correspond to these names (via the same indexes)
- the rest of inputs is about range

What the module does - is to check which values are within range, and according to that - it sends corresponding names (according to indexes) to second output. Is like this:

example range:
95 to 105

example values:
90
96
100
110

example names:
a90
b96
c100
d110

outputs:

96
100

and

b96
c100
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: finding elements in range

Postby Tronic » Wed May 21, 2014 1:12 pm

Code: Select all
min = 95
max = 105
value = [90,96,100,110]
name = ['a90','b96','c100','d100']
a_idx_in_range = []
a_name_in_range = []
a_idx_out_range = []
a_name_out_range = []

value.each_index do |i|
if value[i].between?(min, max)
  a_idx_in_range << i
  a_name_in_range << name[i]
elsif value[i]
  a_idx_out_range << i
  a_name_out_range << name[i]
end
end

watch 'v in range' ,a_idx_in_range
watch 'v out range', a_idx_out_range
watch 'name in range' ,a_name_in_range
watch 'name no range', a_name_out_range


EDIT: schematic in next post :arrow:
Last edited by Tronic on Wed May 21, 2014 1:40 pm, edited 1 time in total.
Tronic
 
Posts: 539
Joined: Wed Dec 21, 2011 12:59 pm

Next

Return to General

Who is online

Users browsing this forum: No registered users and 74 guests