cancel
Showing results for 
Search instead for 
Did you mean: 

Tally array value instances

SOLVED
SimpleJack
Member
Solved!

Tally array value instances

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

11 REPLIES 11
DFGray
NI Employee (retired)

Re: Tally array value instances

Message contains a hyperlink

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).

LandBelenky
Member

Re: Tally array value instances

Message contains an image Message contains an attachment

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

Silver_Shaper
Active Participant
Solution

Re: Tally array value instances

Message contains an image Message contains an attachment

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

 

 

Have a good day..Tally.PNG

---
Silver_Shaper | CLD
Darin.K
Trusted Enthusiast

Re: Tally array value instances

Message contains a hyperlink Message contains an image

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...

SimpleJack
Member

Re: Tally array value instances

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

 

 

altenbach
Knight of NI

Re: Tally array value instances

Message contains an image Message contains an attachment

@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)

 

 

 

 

altenbach
Knight of NI

Re: Tally array value instances

Message contains an image Message contains an attachment

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

 

altenbach
Knight of NI

Re: Tally array value instances

Message contains a hyperlink Message contains an image Message contains an attachment

@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. Smiley Very Happy

 

 

 

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

Darin.K
Trusted Enthusiast

Re: Tally array value instances

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.