Caparing vals to find the most common numbers

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 2 and 3.

Moderators: Benj, Mods

Post Reply
Ondra
Posts: 325
Joined: Wed Aug 29, 2007 7:33 pm
Been thanked: 2 times
Contact:

Caparing vals to find the most common numbers

Post by Ondra »

Good day all. I am working with a sensor unit that has a +/- 1% accuracy over the measured distance. What I am trying to do to
increase my chances of getting a more accurate and consistent value, is to ask the question, which number shows up the most in the
list and then select that value as the correct one. i e: -
int val1=1002
int val2= 902
int val3=1002
int val4 =1102
int val5=1002
int val6=1002
int val7=1002
int val8 = 802
Using something similar to the above I want to compare the values and in this instance I would want to select the number 1002 as the more accurate since it appears more times than the rest. Can someone give me a flowcode example of how I would program for this.
Thanks in advance for your help.

Ondra

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Caparing vals to find the most common numbers

Post by Benj »

Hello Ondra

Best way would be to take an average of the results. To do this create an integer variable and add say 5-10 sets of samples to this variable. Then devide the variable by the number of samples used to get your reliable value.

For your case:
int val1=1002
int val2= 902
int val3=1002
int val4 =1102
int val5=1002
int val6=1002
int val7=1002
int val8 = 802

int average = (1002 + 902 + 1002 + 1102 + 1002 + 1002 + 1002 + 802) / 8 = 977

Ondra
Posts: 325
Joined: Wed Aug 29, 2007 7:33 pm
Been thanked: 2 times
Contact:

Re: Caparing vals to find the most common numbers

Post by Ondra »

Thanks Ben.
I did try averaging the numbers, but The tolerances I am working with still puts the numbers out to far. I need to if possible hit the number on the head. Is there any programming suggestions that would get me the desired answer using the Idea l listed above? It looks to me like a whole mess of "if" blocks. I am hoping there's some kind of programming technique that can run through the numbers, quickly returning the most common value. Thanks again.

Ondra

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Caparing vals to find the most common numbers

Post by Benj »

Hello Ondra

What about something like this.

If you are getting the most common number maybe two or three times in a row then you could create a loop to read in numbers. If the previous number matches the current number then you have a positive fix. Otherwise stay in the loop and update your old and new values. This technique would also work for 3 numbers as long as you can reliably get 3 of the same numbers in a row.

I have attached a quick example using RS232.
Attachments
Valid Input.fcf
(4 KiB) Downloaded 240 times

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Caparing vals to find the most common numbers

Post by Benj »

Sorry just noticed the double equals on the while loop. You will need to make this just one equals to make the code work correctly.

Eg. valid = 0

Sorry been programming too much C recently :)

Ondra
Posts: 325
Joined: Wed Aug 29, 2007 7:33 pm
Been thanked: 2 times
Contact:

Re: Caparing vals to find the most common numbers

Post by Ondra »

Look's like you might have something there. I'm going to play with that. It's giving me some ideas. Another thought, is it possible to create and array of integers or integer array . So that I can index through the values? Give me an example using some of my numbers.


Ondra

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Caparing vals to find the most common numbers

Post by Benj »

Hello Ondra

Yes an array of integers is possible. In the variables window when creating your integer variable give the variable an array size by adding [5] where 5 is the number if integers in the array.

Var_Name[5]

You can then reference the components of the arrays by using calulation icons eg Var_Name[5] would be referenced via the following.

Var_Name[0]
Var_Name[1]
Var_Name[2]
Var_Name[3]
Var_Name[4]

or using an index variable

Var_Name[idx_var_name]

Ondra
Posts: 325
Joined: Wed Aug 29, 2007 7:33 pm
Been thanked: 2 times
Contact:

Re: Caparing vals to find the most common numbers

Post by Ondra »

Very nice. Thanks Ben.

Ondra

Post Reply