finding elements in range

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

finding elements in range

Post by tester »

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

Post by tester »

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.
User avatar
Nubeat7
Posts: 1347
Joined: Sat Apr 14, 2012 9:59 am
Location: Vienna
Contact:

Re: finding elements in range

Post by Nubeat7 »

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
tester
Posts: 1786
Joined: Wed Jan 18, 2012 10:52 pm
Location: Poland, internet

Re: finding elements in range

Post by tester »

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.
User avatar
Nubeat7
Posts: 1347
Joined: Sat Apr 14, 2012 9:59 am
Location: Vienna
Contact:

Re: finding elements in range

Post by Nubeat7 »

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
tester
Posts: 1786
Joined: Wed Jan 18, 2012 10:52 pm
Location: Poland, internet

Re: finding elements in range

Post by tester »

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

Post by tester »

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.
Tronic
Posts: 539
Joined: Wed Dec 21, 2011 12:59 pm

Re: finding elements in range

Post by Tronic »

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
tester
Posts: 1786
Joined: Wed Jan 18, 2012 10:52 pm
Location: Poland, internet

Re: finding elements in range

Post by tester »

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.
Tronic
Posts: 539
Joined: Wed Dec 21, 2011 12:59 pm

Re: finding elements in range

Post by Tronic »

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.
Post Reply