Calculating most repeated number

mosama

Member
Join Date
May 2009
Location
Egypt
Posts
182
Hi every one

I'm using OMRON PLC CPM2A

I have 10 values stored in 10 registers

I want to calculate the most repeated value from the 10 values?

Or tell me if there is any instruction that can do the same job?

Please help as soon as possible
 
Hi every one
I'm using OMRON PLC CPM2A
I have 10 values stored in 10 registers
I want to calculate the most repeated value from the 10 values?...
What means "calculate"?
If you want to find the most repeated value, what do you want as a result?
The value itself or its locations?
What result do you want if there is no repeated value at all?
 
Last edited:
I can think of a couple ways to possibly get the most repeated value, but I doubt they'd be very efficient, or work well on a PLC. This is certainly an interesting problem to solve...
 
What is the range of the values?
It makes no difference.
I can't think of an easy way to do that on a PLC :/
That is right. It will require some brute force compares or looping.
I can think of a couple ways to possibly get the most repeated value, but I doubt they'd be very efficient,
I can't think of any super algorithm. I can think of something that takes about 70 simple rungs doing compares. I it will take about 64 or 65 compares or use two nested for loops.
 
Actually sorting the list would make things a lot easier. You'd compare the number to the next one, if the numbers were the same, that means you have another instance of the same number, if not, then the next one is a new number to start counting. Sorting the list would mean that all the instances of a given value are beside eachother.

My first thought was that sorting it would make things a lot easier. But then I realized that even sorting a list in a PLC is not a trivial thing, lol. I could probably write a bubble sort in my sleep on a computer. But on a PLC, that's a different matter altogether, lol. (Mind you, I'm not exactly experienced at PLC programming.)

However, I think that in the end, if your doing anything more complicated than a bubble sort, it's probably easier just to do a brute force check, lol.
 
Compare-Counter-Compare Solution

Hi every one

I'm using OMRON PLC CPM2A

I have 10 values stored in 10 registers

I want to calculate the most repeated value from the 10 values?

Or tell me if there is any instruction that can do the same job?

Please help as soon as possible

Mosama,

What are you trying to achieve with this? What is the application?
If you're dealing with ten values (ie 1 thru 9)then create a Compare instruction for each value that would increment it's own Counter. Then do some logic that will create an output for whichever Counter value is greater. Then do some more logic if the Counter values are the same etc.

Good Luck ;-)
 
10 counters count the times you write into each register and the biggest counter is the most. oops NO this is not the most used value UMM
65535 counters .......... Not an easy one!!!
Sorry just thinking aloud. He He.
 
The biggest problem here is keeping track of which number Is the most used. It's easy enough to come up with a brute force method of simply counting the number of occurances of each number, but much harder to track which value it is that is repeating the most.
 
I have 10 values stored in 10 registers

I want to calculate the most repeated value from the 10 values?

I am wondering if this is a language translation issue as looking at previous posts
the OP is still on a learning curve and English will not be his Native language.

Mosama is this the question you could have been asking

I want to calculate the AVERAGE value from the 10 values?
 
I don't see a need to bouble-sort them.
I don't know the Omron ST syntaxes, but this should convert pretty easy. It's pretty standard.

This is assuming that CPM2a takes ST and that the values are in an array.

 
int Values[10] = {30,1,30,1,1,30,1,30,30,1}; //Array containing the values (loaded with values)
int Hits[10] = {0,0,0,0,0,0,0,0,0,0}; //Array containing number of hits pr. value
int MostCommon = 0; //The most common number
int MaxHits = 0; //Max number of hits
int EqualHits = 0; //Number of hits with equal frequency


for (int a = 0; a < 10; a++) { //Check how many times each value is repeated
for (int b = a+1; b < 10; b++) {
if (Values[a] == Values) {
Hits[a]++; };

};

};


for (int c = 0; c < 10; c++) { //Find the most frequent
if (Hits[c] == MaxHits) { //Find number of hits with equal frequency
EqualHits++;}

if (Hits[c] > MaxHits){
MaxHits = Hits[c]; //Find max number of hits
MostCommon = Values[c];
EqualHits = 0;
};
};

MaxHits++;



There might be errors. I just compiled and tested it real quick.
 
Last edited:
That is a good start.
This could be done in just one loop by keeping a few more temporary variables.

Code:
MostHitValue=-1;
MostHits=0
for (int a = 0; a < 10-MostHits; a++) { // Don't check if the not possible to exceed MostHits, this may save a lot of time.
  Hits=0
   ValueA=Values[a];   // Avoid indexing every loop
  for (int b = a+1; b < 10; b++) {
    if (ValueA == Values[b]) {
      Hits++ };           // Avoid indexing
   };
  If Hits>MostHits then  // Avoids the extra loop
  {
    MostHits=Hits;
    MostHitValue=a;
   }

};
Anther idea that may work depending on what the numbers look like

I would have a flag to indicate numbers that have been counted. This way you don't look for 30s and 1 after the first two scans. In the example above this may help but if the numbers are all different then it will just slow things down.
 

Similar Topics

This application has a motor with encoder feedback that drives a linear actuator that moves in/out, and is at roughly 45 degs from horiz. As the...
Replies
19
Views
1,415
I need to keep a running pass/fail yield of the previous 5,000 parts produced. I have used this formula before to calculate average: AvgValue =...
Replies
6
Views
2,193
Does anyone know how to calculate the savings from now needing to run an air compressor all day? Basically I have a design that replaced 6 * 1"...
Replies
26
Views
4,917
I would like to refer to this document of which I used some data ...
Replies
1
Views
1,498
Seems like this should be a simple thing but I’m struggling with it. The basis is that I am trying to detect if a linear assembly is stopped or...
Replies
6
Views
3,140
Back
Top Bottom