LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to get number of occurrence and index of multiple number in an 1D array

Solved!
Go to solution

Dear all,

 

I have a question: How can we obtain the number of occurrence and index of each duplicated element in an 1D array?

For example, I have an array: 1,2,3,5,2,1,4,5,3. I want to get the number of duplications and indexes of each element such as: 1: 2 (0,5);2: 2 (1,4);3: 2 (2,8);4: 1 (6);5:2 (3,7).

 

Thank you so much!

0 Kudos
Message 1 of 13
(9,273 Views)

Hi don,

 

there are some approaches possible. What have you tried so far? Where are you stuck?

Mind to attach your VI?

 

Is this homework? It sounds like a very good opportunity to learn LabVIEW and it's features like autoindexing loops and array handling (hint, hint!)…

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 2 of 13
(9,245 Views)

Dear GerdW,

 

This is not a homework. If this is a homework I must have got zero due to deadline. This is a part of my larger project.

 

I have try some approaches now. The attached VI is the one I modified from another program in this forum. This I the best I get now: It gives me the values and corresponding numbers of duplications but not the index. 😞

 

Could you please give me more detail hints?

 

Thank you so much!

 

0 Kudos
Message 3 of 13
(9,227 Views)

Hi don,

 

It gives me the values and corresponding numbers of duplications but not the index.

So you already know, which elements occur and how often.

With this information you can search your input array for each found element to find its indices. All you need is a loop with two inputs: data array and array with "elements found". It still is a nice task to learn LabVIEW.…

 

(This is not the most performant solution, but a practical one. Christian will surely provide the optimized version, after you present your own solution. :D)

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 4 of 13
(9,224 Views)

@GerdW wrote:

(This is not the most performant solution, but a practical one. Christian will surely provide the optimized version, after you present your own solution. :D)


Personally, I'm thinking Variant Attributes where the attribute is an array of indexes.  Create the attribute name based on the value, Get Variant Attribute to get the current array, Build Array to add the current index, and the Set Variant Attribute to write it.  Just make sure the variant is in a shift register.  You can then get all of the attributes with a Get Variant Attribute and do whatever you have to with the final data.  Once you get the array out, it is simple enough to do an Array Size to figure out if it was duplicate and how many times it is in the array.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 5 of 13
(9,217 Views)

The attached VI, Created For one of my earlier task, Hope this may help you.

 

Input ---> Array

Output1--->Occurrence (0th Column Specifies Values and 1st Column Specifies Number of Times Occurred)

Output2--->Location (Place of Occurrence), used Number of times occurred vales to get the actual location from the array.

----------------------------------------------------------------------------------------------------------------
Palanivel Thiruvenkadam | பழனிவேல் திருவெங்கடம்
LabVIEW™ Champion |Certified LabVIEW™ Architect |Certified TestStand Developer

Kidlin's Law -If you can write the problem down clearly then the matter is half solved.
-----------------------------------------------------------------------------------------------------------------
0 Kudos
Message 6 of 13
(9,194 Views)

@GerdW wrote:

 

With this information you can search your input array for each found element to find its indices. All you need is a loop with two inputs: data array and array with "elements found".

 

Dear GerdW,

 

I have tried with your suggestion in the attached VI.

 

I put data array and element found array to a while loop to find the index. But the output Index array only contain the index of the first found element and skip the other indexes. I think for each element the index(es) should be contained in an array but I could not find a way to implement it.

 

Thank you so much!


 

0 Kudos
Message 7 of 13
(9,183 Views)

Dear Palanivel Thiruvenkadam,

 

I can not open your file because it is in LV 2015. I am using LV 2014. Could you please save it in earlier LV version.

 

Thank you so much!

0 Kudos
Message 8 of 13
(9,180 Views)

Modified from earlier attached code,

 

Gives 2D String output

0th Column--Elements

1st Column--Occurence

2nd Column--Location

----------------------------------------------------------------------------------------------------------------
Palanivel Thiruvenkadam | பழனிவேல் திருவெங்கடம்
LabVIEW™ Champion |Certified LabVIEW™ Architect |Certified TestStand Developer

Kidlin's Law -If you can write the problem down clearly then the matter is half solved.
-----------------------------------------------------------------------------------------------------------------
Message 9 of 13
(9,175 Views)

Attached Code is an sorted one so you may not get the elements in same order as your input.

----------------------------------------------------------------------------------------------------------------
Palanivel Thiruvenkadam | பழனிவேல் திருவெங்கடம்
LabVIEW™ Champion |Certified LabVIEW™ Architect |Certified TestStand Developer

Kidlin's Law -If you can write the problem down clearly then the matter is half solved.
-----------------------------------------------------------------------------------------------------------------
0 Kudos
Message 10 of 13
(9,173 Views)