LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Finding the most common value in an array of enums

Solved!
Go to solution

Hello,

 

I'm looking for an elegant way of finding the most common value in an array of enums

 

My enum being:

0 - Close

1 - Open

2 - Undefined

 

For instance, if my array contains:

Close, Close, Open, Close, Close, Open.

The most common value would be "Close"

 

I have created a very customized VI that allows me to obtain the desired result, but I'm really not proud of doing it this way, simply because I would have to modify it if I were to add a new value to my enum...

If someone can share some ideas to enlighten me, I would REALLY appreciate it.

 

Thanks in advance!

Jorge
0 Kudos
Message 1 of 14
(4,505 Views)

Can you simply ask for the median of the array?

0 Kudos
Message 2 of 14
(4,502 Views)

If you have the full version, I would ignore the coercion dots and use the Mode.vi.

0 Kudos
Message 3 of 14
(4,497 Views)
The median does not do what I want.
I have tried the mode VI and it works fine, but makes my app much larger and long to compile. I would like to avoid using it if possible.
Jorge
0 Kudos
Message 4 of 14
(4,484 Views)

There is always the brute force method:

 

EnumArrayMode.png

Message 5 of 14
(4,476 Views)

If the array is not too large where a copy would be an issue you could always sort the array and use a search to look at the index where each one starts. Your loop would probably require fewer iterations than the brute force method.



Mark Yedinak
Certified LabVIEW Architect
LabVIEW Champion

"Does anyone know where the love of God goes when the waves turn the minutes to hours?"
Wreck of the Edmund Fitzgerald - Gordon Lightfoot
Message 6 of 14
(4,470 Views)

@Mark

 

The array could be very very large sometimes. I'll run some benchmarks to check for performance issues,  but your method is pretty darn good nonetheless, thank you 🙂

 

@Darin

 

Brute force or not, it is far better from what I've created, thank you!

 

Jorge
0 Kudos
Message 7 of 14
(4,443 Views)
Solution
Accepted by JorgeF

I am not sure I would go the sorting route, it could in priniciple save a single iteration since you know 0 is the first starting point, but you then have to deal with determining lengths, and dealing with missing values which return -1, etc.

 

If you really expect very large arrays, I would shift from my previous version which is meant to be quick to write to another simple method of counting. 

 

EnumArrayMode_v2.png

Message 8 of 14
(4,422 Views)

Here is the variant using the search method. This method will run N-1 times where N is the number of ENUM elements. Yes, it is a bit more complex than the brute force method but it would execute fairly quickly. The sort would probably be the time consuming task.

 

ENUM Counter.png



Mark Yedinak
Certified LabVIEW Architect
LabVIEW Champion

"Does anyone know where the love of God goes when the waves turn the minutes to hours?"
Wreck of the Edmund Fitzgerald - Gordon Lightfoot
Download All
0 Kudos
Message 9 of 14
(4,407 Views)

Follow up. At face value it seems like the search method would be faster and it is provided you ignore the sort time. The overall performance is better with the with the brute force method. A test of an array with 1000000 elements showed the brute force method took 9 ms. The search method took 98 ms. However 94 ms of that time was the sort.



Mark Yedinak
Certified LabVIEW Architect
LabVIEW Champion

"Does anyone know where the love of God goes when the waves turn the minutes to hours?"
Wreck of the Edmund Fitzgerald - Gordon Lightfoot
Message 10 of 14
(4,402 Views)