Page 1 of 2
min/max filtering (array)
Posted: Sat Nov 09, 2013 12:22 am
by tester
One of previous topics created another one.
Within array, how to filter values within min/max range?
Let say that min=0, max=200
then from values: -1,2,40,100,400
only these in min/max range are on output.
I guess this can be done by filtering all above and below, or by pushing through all within minmax range.
I'm reading these ruby docs, but can't understand how to get it (I even have no idea whether I'm reading the right thing).
Re: min/max filtering (array)
Posted: Sat Nov 09, 2013 12:39 am
by billv
You can try the nubeat7 class array thing....
http://www.dsprobotics.com/support/viewtopic.php?f=3&t=1760#p8243It's got a min/max for array in there....
Re: min/max filtering (array)
Posted: Sat Nov 09, 2013 1:08 am
by tester
I guess you mean this part
Code: Select all
def limit (minVal,maxVal)
return self.map{|item|[[item,minVal].max,maxVal].min}
end
I'm not sure how to put it to work (ins/outs as usual]
Re: min/max filtering (array)
Posted: Sat Nov 09, 2013 1:37 am
by Nubeat7
just put the module inside your schematic ( before any other ruby module)
and you can use it like
Code: Select all
output @yourarray.limit(minVal,maxVal)
if you dont want to use the methode expansion module you have to rewrite the methode from a class methode (the stuff with self) to a "normal" methode where self would be your array:
Code: Select all
def limit (minVal,maxVal)
return self.map{|item|[[item,minVal].max,maxVal].min}
end
would be
Code: Select all
output @yourArray.map{|item|[[item,minVal].max,maxVal].min}
to use it like the class methode without putting the expansion module inside the schematic you also could declare the methode in your ruby module like:
Code: Select all
class Array
def limit (minVal,maxVal)
return self.map{|item|[[item,minVal].max,maxVal].min}
end
end
and u can use it like this :
Code: Select all
output @yourarray.limit(minVal,maxVal)
too!
Re: min/max filtering (array)
Posted: Sat Nov 09, 2013 1:51 am
by Nubeat7
but sorry i didnt read the first post well, just answered to billvs idea.. the above methode describes how to set all values inside an array to min /max values (limits the values!) to show just the values which are in a given range you need a different methode... but sorry too tired atm.. have to work tomorrow but search keep_if here:
http://ruby-doc.org/core-2.0.0/Array.html
Re: min/max filtering (array)
Posted: Sat Nov 09, 2013 3:59 am
by billv
Got it tester....took longer than expected..
Gives exact same output as standard green min/max...
Float array input and output...
Code: Select all
def event i,v
if i == 0 then
data=[]
x=0
minVal = 0.5
maxVal = 1
a = maxVal - minVal
@list.length.times do |x|
data[x] = @list.map{ |x| x.to_f * a + minVal }
x += 1
end
output 0,data[0]
watch data
end
end
Re: min/max filtering (array)
Posted: Sat Nov 09, 2013 5:20 am
by billv
This is a single float version...
Code: Select all
def event i,v
if i == 0 then
minVal = -0.5
maxVal = 0.5
a = maxVal - minVal
b = @txt * a
c = b + minVal
output 0,c
end
end
@nubeat7
Did have a lot of trouble using
Code: Select all
.map{|item|item*maxVal-minVal+minVal}
dosn't seem to happen with the "maxVal-minVal" where it is...
if you follow the flow of standard green min/max..
the "maxVal-minVal" dosn't belong inside main calculation...so it sort of makes sense
Re: min/max filtering (array)
Posted: Sat Nov 09, 2013 9:12 am
by Nubeat7
@tester
this should do it:
Code: Select all
output @yourarray.keep_if{|x|x>minValue && x<maxValue}
billv wrote:@nubeat7
Did have a lot of trouble using
CODE: SELECT ALL
.map{|item|item*maxVal-minVal+minVal}
dosn't seem to happen with the "maxVal-minVal" where it is...
if you follow the flow of standard green min/max..
the "maxVal-minVal" dosn't belong inside main calculation...so it sort of makes sense
what you are trying to do here is scaling the values to min max range.. look inside my expansion module the methode calls "scale_values"
Re: min/max filtering (array)
Posted: Sat Nov 09, 2013 9:45 am
by Tronic
just one line method
Code: Select all
# range method (n..n) === N
val_to_test_in_range = @in;
val_if_in_range = @in ;
val_if_not_in_range = 0.0 ;
min = 0.0 ;
max = 0.5 ;
is_val_in_range = (min..max) === val_to_test_in_range ? val_if_in_range : val_if_not_in_range ;
... with array
Code: Select all
array = [-1,0.0,90,50,100,150,200]
min = 0.0 ;
max = 100 ;
(array.map {|v| (min..max) === v ? v : nil }).compact
Re: min/max filtering (array)
Posted: Sat Nov 09, 2013 10:24 am
by billv
Nubeat7 wrote:what you are trying to do here is scaling the values to min max range.. look inside my expansion module the methode calls "scale_values"
I actually posted the limiter fix for tester from your fsm,
but for some reason i deleted it and changed it to scale..
While the limiter works fine...
Code: Select all
def event i,v
if i == 0 then
minVal = 0
maxVal = 1
a = @txt.map{|item|[[item,minVal].max,maxVal].min}
output 0,a
end
end
I still can't get a result from this...
Code: Select all
def event i,v
if i == 0 then
minVal = 0.5
maxVal = 1
a = @txt.map{|item|item*maxVal-minVal+minVal}
output 0,a
end
end