Page 1 of 2

finding elements in range

Posted: Mon May 19, 2014 11:04 pm
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?

Re: finding elements in range

Posted: Mon May 19, 2014 11:28 pm
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.

Re: finding elements in range

Posted: Mon May 19, 2014 11:35 pm
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

Re: finding elements in range

Posted: Tue May 20, 2014 7:01 am
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.

Re: finding elements in range

Posted: Tue May 20, 2014 12:20 pm
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

Re: finding elements in range

Posted: Tue May 20, 2014 11:20 pm
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.

Re: finding elements in range

Posted: Wed May 21, 2014 12:11 am
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

Re: finding elements in range

Posted: Wed May 21, 2014 9:25 am
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

Re: finding elements in range

Posted: Wed May 21, 2014 12:37 pm
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

Re: finding elements in range

Posted: Wed May 21, 2014 1:12 pm
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: