Example Code

Search 1D Array for Nearest

Products and Environment

This section reflects the products and operating system used to create the example.

To download NI software, including the products shown below, visit ni.com/downloads.

    Software

  • LabVIEW

Code and Documents

Attachment

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: 

  
Search 1D Array for Nearest FP.png

 

Search 1D Array for Nearest sn.png


**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.

Comments
Logical2u
Member
Member
on

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.

David_L
Active Participant
Active Participant
on

Well I feel silly... That was pretty obvious.  Made the fix now.  Thanks for pointing that out!

Karthikeyan_Mathiyalagan
Member
Member
on

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.... !!!!!!!!!!!!!!

David_L
Active Participant
Active Participant
on

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!

ashifulk
Member
Member
on

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.

David_L
Active Participant
Active Participant
on

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.

ashifulk
Member
Member
on

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.

albe0186
Member
Member
on

Useful VI David. Thanks

bets855
Member
Member
on

Does anyone know how to alter this for a 2D array?

David_L
Active Participant
Active Participant
on

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.

bets855
Member
Member
on

Is this possible even when looking for a desired point instead of a desired element?

David_L
Active Participant
Active Participant
on

I'm not sure what you mean by a "desired point'.

bets855
Member
Member
on

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.

David_L
Active Participant
Active Participant
on

Oh ya, tha sounds like something totaly different , sorry.


Good luck!

Andri_andrei
Member
Member
on

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