07-01-2013 03:09 PM
I would like to build a simple VI to find the largest N items in an array with the index of each item.
07-01-2013 03:11 PM
@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?
07-01-2013 03:17 PM
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.
07-01-2013 03:58 PM
@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.
07-01-2013 04:26 PM
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)?
07-01-2013 04:50 PM
@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
07-01-2013 07:01 PM - edited 07-01-2013 07:04 PM
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
07-02-2013 01:27 AM
@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
07-02-2013 05:55 AM
@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