Example Code

Find the Mode of an Array of Integers

Code and Documents

Attachment

Download All

Overview:
This VI finds the mode of an array of integers.
Description:

Since the current VIs used for finding the mode do so on a set of double values, they don't take the exact value of each element into account.  Instead they use buckets to find the mode.  However, this can give odd results when wiring an array of integers to these VIs.  The value will generally come out as a decimal value close to the true mode, which may not be expected.  Here is a VI that finds the true mode(s) of an array of integers:

1. Sort the array so that we can count consecutive elements of the same value.  Also, initialize our count and value arrays.
2. Check if the current element is equal to the last one we looked at.
3. If it is, then increment count.  Otherwise, reset the counter to one (we've seen one of this current value) and add the last value, count pair to the arrays.
4. Add the last value, count pair to our arrays since it isn't caught in the previous loop.  Also find the max of the count array.
5. Find all values that had the max count and add them to our mode(s) array.
6. Strip the last element added, since it is bogus data.

Requirements:

  • LabVIEW 2012 (or compatible)

Steps to Implement or Execute Code:

  1. Run the VI.

 Additional Information or References:
Screenshot_4.png

 

 

 

 "**This document has been updated to meet the current required format for the NI Code Exchange.**" 

 
Jared S.
Applications Engineering
National Instruments

Example code from the Example Code Exchange in the NI Community is licensed with the MIT license.