min/max filtering (array)

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

min/max filtering (array)

Post 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).
Need to take a break? I have something right for you.
Feel free to donate. Thank you for your contribution.
billv
Posts: 1165
Joined: Tue Aug 31, 2010 3:34 pm
Location: Australia
Contact:

Re: min/max filtering (array)

Post by billv »

You can try the nubeat7 class array thing....
http://www.dsprobotics.com/support/viewtopic.php?f=3&t=1760#p8243
It's got a min/max for array in there....
tester
Posts: 1786
Joined: Wed Jan 18, 2012 10:52 pm
Location: Poland, internet

Re: min/max filtering (array)

Post 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]
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: min/max filtering (array)

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

Re: min/max filtering (array)

Post 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
billv
Posts: 1165
Joined: Tue Aug 31, 2010 3:34 pm
Location: Australia
Contact:

Re: min/max filtering (array)

Post by billv »

Got it tester....took longer than expected.. :?
Gives exact same output as standard green min/max... :D
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
billv
Posts: 1165
Joined: Tue Aug 31, 2010 3:34 pm
Location: Australia
Contact:

Re: min/max filtering (array)

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

Re: min/max filtering (array)

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

Re: min/max filtering (array)

Post 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
billv
Posts: 1165
Joined: Tue Aug 31, 2010 3:34 pm
Location: Australia
Contact:

Re: min/max filtering (array)

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