4/19/2011 1:59 PM  
Joined: 8/27/2010 Last visit: 4/13/2024 Posts: 417 Rating: (37) 
Good day. I have 5 values, coming each 2 seconds (e.g. flow or volume) How can i compare all 5 values and get only one (middle) of them? There is 2 ways how to make it: 1. delete lowest and highest values to get middle. 2. summ all 5 values and divide result by 5. How to realise it in STL? Any ideas or used examples? Thanks a lot. 
4/20/2011 8:27 AM  
Posts: 270 Rating: (53) 
Hello olloy, first, I don't think the second option is of any use, unless you actualy want an average value, because arithmetical mean does not (always) equal median (which is what you want to get, afaik). So, I'm pretty sure there are multiple ways to do this, one is the first option you suggested, or you can just sort those 5 values and pick the 3rd with some sorting algorithm or you can just load them into 5 separate variables and compare them inbetween, I guess you should be able to optimize it to just few comparations. Also, it is quite important to realize, whether you actually really look for median or you really just need to get an average mean, which you really can just sum the five elements with simple loop and then divide it by 5, which is, I suppose, best solution for such case. 
4/20/2011 11:36 AM  
Posts: 270 Rating: (53) 
Once again hi olloy, I was thinking a bit about the case where you would need to compare the 5 numbersinbetween them (I asume using a sorting algorithm on that is a bit of an overkill, as it would spent probably most time on initialization and stuff) and I got down to6 necessary comparisons to determine the middle number of the three (or5 in the "good" case), so that might perhaps be the fastest way to do so, maybe someone can think of even less? 
4/20/2011 11:58 AM  
Joined: 8/27/2010 Last visit: 4/13/2024 Posts: 417 Rating: (37) 
Hello. Thanks for the answers. Yep, probably u right and in case of 5 primary given values to get one midvalue, six comparisons will be smallest possible. Dont you have some code example for this application? In my case i will use the result for informationa purposes of tankerloading speed (qubic meters per hour), and if thr digit will be inaccurate for 10 percents  it is enough good for me. Yhanks a lot once again. ________ edit: actually i was thinking, that step7 have some sfc block for that purposes. this task is very often needs to be solved... you have x, y, z and need to get middle value. 
Last edited by: olloy at: 4/20/2011 11:59 AM 

4/20/2011 1:26 PM  
Posts: 270 Rating: (53) 
Well if there's such function, that should be fairly easy then. I don't have any exact code for my solutions, but you can just craft it following the algorithm to obtain the result (save them into variables and then compare the variables and branch the code as the algorithm goes). If you got that function to pick middle one of three numbers, you should just use it twice ([1]middle of ([2]middle of (A, B, C) D, E)and then compare smaller of [2] with middle of [1], but that's just wild guess with not all combinations checked. For the case of single number comparison, I actualy got a little confused now, because I realized there was some flaw and you might need 7 rather than only 6 comparisons, I'll try to get to it later, now I really can't even tell if 2 is bigger than 1 or vice versa :D ... There might though be some algorithm that solves this somewhere on the internet, I wouldn't be surprised if there is one, at least, so you might look into that also. 
4/20/2011 2:29 PM  
Posts: 316 Rating: (37) 
Hi, 
4/20/2011 2:58 PM  
Joined: 8/27/2010 Last visit: 4/13/2024 Posts: 417 Rating: (37) 
thank you, but looks like anothe member found easiens way (second way). take 5 values, summ them and divide summ by 5. many rows, but it will work (i hope) ^)

4/20/2011 2:59 PM  
Joined: 8/27/2010 Last visit: 4/13/2024 Posts: 417 Rating: (37) 
thanks, garfield, i'll try it after holidays. 
4/20/2011 3:18 PM  
Posts: 270 Rating: (53) 
Well, that is exactly what Chilly_Garfield did there, he just asumed you have the values in discrete variables, rather than an array or some other data sequence, which you can iterate through to sum. I asume also, that you wanted an average mean then, not the median (which is"middle value" in case of odd amount of numbers). Anyway, good luck with whatever are you gonna do with either of those solutions, you're probably gonna need it. 
Follow us on