LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

N Largest Array Items with Indices

I would like to build a simple VI to find the largest N items in an array with the index of each item.

0 Kudos
Message 1 of 9
(2,789 Views)

@ouhaoran wrote:

I would like to build a simple VI to find the largest N items in an array with the index of each item.


That's nice.  Did you have a question?


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
Message 2 of 9
(2,788 Views)

LOL (crossrulz) too funny...

 

 

ouhaoran, have you tried the "Sort 1D array" VI ?

Largest X values (from N sized array) = N-X-1 to N-1.

0 Kudos
Message 3 of 9
(2,785 Views)

@Don_Phillips wrote:

LOL (crossrulz) too funny...


But no kudo?

 

 


Don_Phillips wrote:ouhaoran, have you tried the "Sort 1D array" VI ?Largest X values (from N sized array) = N-X-1 to N-1.


Notice that the OP also needs the index.  So if you go that route, you first need to bundle the element and its index so you have an array of clusters.  You can then sort array of clusters and grab the first N elements of that array.

 

The method I was considering was to do this in a FOR loop.  Keep the array in a shift register.  Use the Max & Min function to get the max value and its index.  Use Replace Array Subset to set that index to the lowest possible number for your data type.  You can then repeat to get your N elements.


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 4 of 9
(2,773 Views)

Thanks, crossrulz. The bundling idea makes sense. Will Labview automatically sort by the first item in the bundle (if the first item is the value at the array index, say)?

0 Kudos
Message 5 of 9
(2,765 Views)

@ouhaoran wrote:

Thanks, crossrulz. The bundling idea makes sense. Will Labview automatically sort by the first item in the bundle (if the first item is the value at the array index, say)?


Yes, Sort on a cluster sorts by element 0 first then element 1 only if element 0's are the same


"Should be" isn't "Is" -Jay
0 Kudos
Message 6 of 9
(2,752 Views)

An alternative is to use the Sort Array and then the Reverse 1D Array functions. Using a FOR loop take the N first values (the N largest values) and then use the Search 1D Array function the find the indices of these elements in the original array. You can use the FOR loop to build a 2D array where the first column is the element of the first array and the second its index in the first array.

 

Ben64

0 Kudos
Message 7 of 9
(2,731 Views)

@ben64 wrote:

An alternative is to use the Sort Array and then the Reverse 1D Array functions. Using a FOR loop take the N first values (the N largest values) and then use the Search 1D Array function the find the indices of these elements in the original array. You can use the FOR loop to build a 2D array where the first column is the element of the first array and the second its index in the first array.

 

Ben64


The problem with this solution being that Search 1d array get the 1st match, if you e.g. have 3 20's you'll miss 2.

 

/Y

G# - Award winning reference based OOP for LV, for free! - Qestit VIPM GitHub

Qestit Systems
Certified-LabVIEW-Developer
Message 8 of 9
(2,703 Views)

@Yamaeda wrote:
The problem with this solution being that Search 1d array get the 1st match, if you e.g. have 3 20's you'll miss 2.

 

/Y



You're right. I missed that.

 

Ben64

0 Kudos
Message 9 of 9
(2,690 Views)