03-19-2018 10:06 PM
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!
Solved! Go to Solution.
03-20-2018 02:32 AM
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!)…
03-20-2018 09:21 AM
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!
03-20-2018 10:04 AM - edited 03-20-2018 10:05 AM
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)
03-20-2018 11:55 AM
@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.
03-21-2018 01:25 AM - edited 03-21-2018 01:26 AM
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.
03-21-2018 02:13 AM
@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!
03-21-2018 02:16 AM
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!
03-21-2018 02:20 AM
Modified from earlier attached code,
Gives 2D String output
0th Column--Elements
1st Column--Occurence
2nd Column--Location
03-21-2018 02:22 AM
Attached Code is an sorted one so you may not get the elements in same order as your input.