From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Tally array value instances

Solved!
Go to solution

Hi,

 

I need a little insight on this.....

 

If I have a 1D array of integer values, what woudl be the easiest way to tally up the quantites of same numbers (not addition)?

 

For example, my 1D array might be this:  1,6,4,55,23,6,2,55,23,23

 

I would like to report the quantites in 2D array (or not an array, whatever is easiest):

 

1     1

6     2

4     1

55   2

23   3

 

I'm not sure how to do this simply.....I see that search array gives the element number and only the first element it sees with that value. I just need pointed in the right direction.

 

Thanks,

SJ

0 Kudos
Message 1 of 12
(3,693 Views)

There is a certain amount of complexity involved with solving this.  I would do it as follows.

 

  1. Create two empty one dimensional arrays, one of which will contain the values, the other will contain the counts.
  2. Use the two arrays to initialize two shift registers of a FOR loop.
  3. In the FOR loop, loop over your original array.
  4. At each iteration, search for the current value of your original array in the value array in the shift register.  If you find it, increment the corresponding value in the count array.  If you do not find it, add a new entry to the value and count arrays.

At the end, you will have a value array containing all the values and a count array containing the counts at each value.  Note that this particular algorithm is straightforward, but will be slow for huge arrays.  See this post for inspiration on how to speed it up (a lot).

0 Kudos
Message 2 of 12
(3,690 Views)

here are two different ways to do it.

 

The top one uses the Histogram function to count the number of elements in bins of size=1.  (then you have to go through and clean up the arrays to eliminate empty bins.)

 

The botom solution is similar to the comment above.  It uses the 'Search 1D array' to determine if an element already is in the array.  If the element is there, the Count of the element is increased by one.  If it is not there, it is added to the array with a count of 1.Histogram.jpg

0 Kudos
Message 3 of 12
(3,674 Views)
Solution
Accepted by topic author SimpleJack

Dear Jack. Please find the attached vi and image to solve your problem.. 

 

 

Have a good day..Tally.PNG

---
Silver_Shaper | CLD
Message 4 of 12
(3,669 Views)

I would try something like this:

 

TallyArray.png

 

Sort & Reverse array.  Start at last element, search for it from the beginning.  Difference between locations gives number of elements.  Jump to next value and repeat as necessary.  I have used the built-in primitive to search the 1D array, since I spent the effort to sort the array I would normally use my personal search using bisection which can be much faster.  An excuse to agitate again for the following:

 

http://forums.ni.com/t5/LabVIEW-Idea-Exchange/quot-Search-Array-quot-Add-quot-array-is-sorted-quot-b...

0 Kudos
Message 5 of 12
(3,655 Views)

Sazi,

 

This worked great. This is exactly what I was looking to do.I never thought of trying this approach. Thank you very much.

 

Thanks everyone for the great ideas. Much appreciated.

 

Ryan

 

 

0 Kudos
Message 6 of 12
(3,621 Views)

@SimpleJack wrote:

I would like to report the quantites in 2D array (or not an array, whatever is easiest):


If the elements are non-negative integers and in a relatively narrow range, you could just create a simple 1D array where the index is the number and the value is the count. Here is an example where the result is shown on a histogram. (I also attached a snippet)

 

 

 

 

Download All
0 Kudos
Message 7 of 12
(3,613 Views)

Here's a simple version that does the 2D array output as describved in the original question. Try it!

 

Download All
0 Kudos
Message 8 of 12
(3,593 Views)

@Sazi wrote:

Dear Jack. Please find the attached vi and image to solve your problem.. 


I am wondering what would happen of you remove the "equal false" and swap the inputs to the select node, even eliminating a crossed wire. 😄

 

 

 

An "equal false" is just an invert, right? See also this long thread... ;))

0 Kudos
Message 9 of 12
(3,583 Views)
You are so focused on the Rube that you miss the fact that the entire point of that segment of code is to put a value into an array that is simply going to be deleted by the bucket brigade in the next loop. Not to mention building a large array in one loop to be pared down in a following loop.
0 Kudos
Message 10 of 12
(3,576 Views)