To download NI software, including the products shown below, visit ni.com/downloads.
Overview
This is a specialized array search which looks for the element closest to the input element.
Description
The LabVIEW function "Search 1D Array" will search through an array for an exact element and return the index in the array where this value is located. This VI has similar functionality, however it will find the index of the element in the array that is the closest to the input element.
Requirements
LabVIEW 2012 (or compatible), Hardware: none.
Steps to Implement or Execute Code
Use the attached VI as a subVI or test the code using its front panel.
Additional Information or References:
**This document has been updated to meet the current required format for the NI Code Exchange.**
Example code from the Example Code Exchange in the NI Community is licensed with the MIT license.
Your closest element returns the closest element to zero from the array w/value subtracted. I can't seem to upload an image, but the fix is simple - just wire the 1D array input to the "index array" VI instead of the operated-upon array. This gives the value in the array closest to the desired element.
Well I feel silly... That was pretty obvious. Made the fix now. Thanks for pointing that out!
Nice Logic David,My question is do we need Array Subset , we can wire directly to subtraction terminal know...... and why should we need to check <0 index we can directly wire the min(index) to closest index know.... !!!!!!!!!!!!!!
Thanks Karthiikeyan, Those two extra things you mention are there so the function behaves just like the search 1D array function in LabVIEW. The Array subset is so we can use a start index, and the <0 is to check if the input array is an empty array. However, thanks for double checking my work!
Is there a way to modify this code so it outputs the two cloest values? For example, I want to search for the cloest value to 5.2 in an array and the array contains 0, 2, 5, 5.1, 5.3. I would like the function to return both 5.1 and 5.3. I'm sorry if this ins't the place to ask this question.
This sounds very possible. Easiest solution is probably to run this code once, remove the "closest" value from the original array and then run the code a second time on the new array. However I'm sure there is a more elegant solution that doesn't modify the original array if this is needed.
Thank you for the fast response David_L. I actually did that (removing the cloest value from the original array and then rechecking the "new" array). I was looking for something that might be better but this method does work.
Useful VI David. Thanks
Does anyone know how to alter this for a 2D array?
You can probably just reshape the 2D array to a 1D array and then pass it into this VI. You'll need to do a little math to translate the index from the 1D index to the 2D index, but can't imagine it's too hard.
Is this possible even when looking for a desired point instead of a desired element?
I'm not sure what you mean by a "desired point'.
Instead of looking for an exact element inside the array, I am searching for the point (x or row, y or column) location closest to a disired point location. I think I have may have mistaken what this vi does though, whoops! Thanks for the quick response.
Oh ya, tha sounds like something totaly different , sorry.
Good luck!
How to avoid to get the element you are looking for if it is already in the array?
Example if you search for 3 but 3 is in the array but you want to get the closest value of 3