From 04:00 PM CDT – 08:00 PM CDT (09:00 PM UTC – 01:00 AM UTC) Tuesday, April 16, 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: 

Count multiple element in a 1D array

Solved!
Go to solution

Hi LV warrior.

I have a problem concerning manipulating data of an array.

 

I have an array of elements(numbers), and I want to count the different elements in an array.

Example : Array of  (1,1,1,1,2,2,2,3,3,3,3,3,5,5,5)

I want to have at the end a listbox showing that we have :

4 Times 1

3 Times 2

5 Times 3

3 Times 5

 

 :smileyfrustrated:  I have no idea how. Any insight would be helpful..

Thanks


 

Uhmmmm.. Guys should we maybe move this to the rube goldberg thread?

 

You might think of that as a "Histogram"

 

01.png


"Should be" isn't "Is" -Jay
0 Kudos
Message 11 of 19
(1,507 Views)
Solution
Accepted by topic author syahid89

Slight rounding problem in the first attempt-fixed

 

(Dang Bankers)

Slight Rounding problem fixed.png


"Should be" isn't "Is" -Jay
Message 12 of 19
(1,498 Views)

@syahid89 wrote:

Hei thanks for the response (I Guess)

 

My Idea is to split the array containing different element, then calculate size of every newly formed array.

But I do think this idea is not effective, and there must be easier way to do this.

 


As an NI employee, shouldn't you already be getting LabVIEW training as part of your on the job training?

0 Kudos
Message 13 of 19
(1,481 Views)

syahid89 wrote:

have an array of elements(numbers), and I want to count the different elements in an array.

Example : Array of  (1,1,1,1,2,2,2,3,3,3,3,3,5,5,5)

I want to have at the end a listbox showing that we have :

4 Times 1

3 Times 2

5 Times 3

3 Times 5


Can we make assumptions about the datatype and range of possible numbers?

Message 14 of 19
(1,455 Views)
Solution
Accepted by topic author syahid89

If the numbers are relatively small positive integers, you can increment an array where the index represents the number and the value represents the count. (top of image). THis is a very fast solution that operates in-place.

 

In the more general case, I would recommend the variant solution, but in a slightly different way than mentioned earlier. Just use typecasting for the name (formatting numbers in decimal text seems like a complicated detour!). (bottom of image). It can easily be used for arrays with almost any datatype (of course the final formatting would require some adaptation).

 

 

Download All
Message 15 of 19
(1,444 Views)
Solution
Accepted by topic author syahid89

@altenbach wrote:

...  It can easily be used for arrays with almost any datatype (of course the final formatting would require some adaptation).


Here's a quick example where the array elements are clusters of a I16 and a DBL. Works equally well.

 

 

Download All
0 Kudos
Message 16 of 19
(1,431 Views)

If the array elements contain variable-lenght fields, we need to replace the typecase with flatten/unflatten (else we get a broken wire, of course). The rest stays the same. 😄

 

 

Download All
0 Kudos
Message 17 of 19
(1,425 Views)

Yes I have. 

However as a manufacturing guy , I am more familiriased to read the code, not to build ithe algorithm.

I was hoping to learn more and by joining the forum, it may help me to see different ways of coding.

This is what I came up with the first time :

 

Code Count Element Array.jpg

Then I realise there must be a better way than to use 2 "for loops". 

 

So I come out here to ask if there's some other way and so far I have seen a few proposal albeits the "simpletone-ness of my question". 

Quite fruitful 🙂

Thanks 

Message 18 of 19
(1,355 Views)

These are great exercises to play with LabVIEW structures and think about a problem. 

 

Just looking at your image, there are a few points you should learn. For example, "index array" is resizable and if you wire the first index terminal, the next index is assumed to be one higher. All you need in the first loop is a single index array and there is no need to calculate the +1.

There are a few logical flaws. What comes to mind immediately is that on the last iteration you run out of elements on the second index and thus get a zero returned. If the sorted array ends in a collection of zeroes, you might count one extra. No way to tell because we cannot see the other case, etc.

Why would you need to split the array at position X to measure the size of the resulting array? Shouldn't you be able to calculalte the lenght from the value of X directly? 😄

 

A good exercise would be to start with sorting the array, then measuring the lenght of each strech with identical elements. It can definitely be made much simpler and efficient than your code. Try it! 😄

Message 19 of 19
(1,343 Views)