From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to find index of nearest value in array

Solved!
Go to solution

Hello,

i have a double-array A and a double value B.

How can i find out which number in the array A is the nearest number to the number B?

Thanks for help

0 Kudos
Message 1 of 8
(6,556 Views)

There's a few different ways to do this. The most efficient will depend on the properties of array A.

Is array A sorted? If so, I think a threshold and final compare will be your best bet.

If not, you can brute force it by calculating a distance array and taking the min value from there.

0 Kudos
Message 2 of 8
(6,545 Views)

The array-size is only 10 values.

How do i calculate a distance -array?

0 Kudos
Message 3 of 8
(6,543 Views)

I guess I'd do this as a distance squared array.

distance squared array = (A-B)^2

 

Then do a Min-Max on the distance squared array to find the index of minimum distance. That index on your A array is the closest numeric value to B.

 

With just 10 values, your other alternative would be to sort A, then do a threshold of A using B to find the transition index. Compare either side to find the minimum.

 

 

0 Kudos
Message 4 of 8
(6,537 Views)

You aren't giving many details, or answering the questions already asked.  But still I think this is easy enough.  As mentioned if the arrays are sorted you can probably optimize it.

0 Kudos
Message 5 of 8
(6,532 Views)

Thanks you for trhe example.

My B is only a value, not array. Only A is an array.

 

0 Kudos
Message 6 of 8
(6,522 Views)

Hi One,

 

you already have more than 700 messages in the forum so I guess you already left the "absolute beginner" status (regarding LabVIEW) behind you.

 

But still you are not able to calculate the minimal difference of an array and a scalar number?

check.png

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
Message 7 of 8
(6,514 Views)
Solution
Accepted by OnlyOne

If the array is sorted, the built in function "Threshold 1D Array" might work for you.

"If you weren't supposed to push it, it wouldn't be a button."
Message 8 of 8
(6,453 Views)