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

 

 Smiley Frustrated  I have no idea how. Any insight would be helpful..

Thanks

Message 1 of 19
(6,034 Views)

@syahid89 wrote:

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

 

 Smiley Frustrated  I have no idea how. Any insight would be helpful..

Thanks


 

 

Start learning how to program in LabVIEW.

http://www.ni.com/academic/students/learn-labview/

0 Kudos
Message 2 of 19
(6,026 Views)

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.

 

0 Kudos
Message 3 of 19
(6,018 Views)

You can use variant attributes. Take a look at this thread 'How to find the mode of an array of strings'.

You just need to convert the numbers in your array to strings because the variant attributes are strings.

0 Kudos
Message 4 of 19
(5,998 Views)

Suppose I gave you a list of numbers --- 2, 2, 2, 5, 5, 5, 5, 7, 7 -- and I asked you how many of which numbers there are (but I didn't tell you the answer).  Could you tell me?  Asssuming that the answer is "Yes", how did you work it out?  What steps did you use?  Can you write down the steps?  [This is called an algorithm].

 

Once you have the steps written down (in your native language), can you see how to do any part of any of the steps using LabVIEW?

 

Leave the last part of your problem (displaying the answer in a listbox) for last -- figure out the algorithm first.  It is basically a counting problem ...

 

Bob Schor

Message 5 of 19
(5,975 Views)

Here's a sample of two ways that were mentioned before. Try to get the most of them, and maby find a better solution?

0 Kudos
Message 6 of 19
(5,934 Views)

This problem would be a nice candidate for a Code Miniature.  Building an array in a loop disqualifies my solution if you might end up with large arrays.

 

count array items.png

Jim
You're entirely bonkers. But I'll tell you a secret. All the best people are. ~ Alice
For he does not know what will happen; So who can tell him when it will occur? Eccl. 8:7

Message 7 of 19
(5,915 Views)

Another way using OpenG functions 🙂

 

 

count.png

 

 

 

Message 8 of 19
(5,880 Views)

@sandal wrote:

Another way using OpenG functions 🙂 


That's the way I was thinking.  It won't be the most efficient, but it is usually good enough, and besides everyone already has OpenG installed anyway right....right guys?

 

Also the OpenG version has polymorphic support for various data types.

Message 9 of 19
(5,873 Views)
Solution
Accepted by topic author syahid89

Here is a version which generates a 2D array as output. The first column has the counts and the second column has values. It sorts the input array so there is no restriction on the order of the elements in the input array.  Limitation: Integer data types.

 

The second case structure handles the two cases where the last two elements in the sorted array are the same or different.

 

Credit to Bob Schor: After thinking about the issue for a bit I wrote down the algorithm before I started coding.  The algorithm is not spelled out in the VI.

 

Lynn

Message 10 of 19
(5,829 Views)