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: 

output array index from comparing input value to array value

Hi,

i am trying to get an array index value by comparing an input value to the values in the array.  the closest value in the array to the input would result in the output being the index of the array. 

for example

array value        index

0                            0
7                            1
15                          2
30                          3

If my input was number 17, then i would want the output index to 2.   i attached a vi with the  array , input, and output blocks, but i can't figure out how to efficiently solve this.

thank you
0 Kudos
Message 1 of 4
(2,453 Views)

Hi Ifoitek,

      Rounding the result of "Threshold 1D Array" (see Array pallet ) should do it!

Cheers

"Inside every large program is a small program struggling to get out." (attributed to Tony Hoare)
Message 2 of 4
(2,448 Views)
(The function "threshold array" only works for the very special case of an array sorted in non-descending order. So, depending on your array, it may, or may not give the correct result :().
 
In the general case, just take the absolute difference between [array] and [input], then get the index of the array minimum 🙂 . See attached (LabVIEW 8.0).
 

(Of course of you have multiple elements that are equally close, finding all might need a bit more code. 😉 The above finds only the one with the lowest index).

 

Message Edited by altenbach on 01-13-2007 04:17 PM

Download All
Message 3 of 4
(2,445 Views)


@altenbach wrote:
(The function "threshold array" only works for the very special case of an array sorted in non-descending order. So, depending on your array, it may, or may not give the correct result :().

You're right (of course Smiley Wink ).  I shouldn't have assumed Ifoitek's data would always be in ascending order, just because his example was.  When you're on the field and catch these slips, it reminds/motivates one to work harder - not to let them slip by.

Cheers. Smiley Happy

Message Edited by tbd on 01-13-2007 07:21 PM

"Inside every large program is a small program struggling to get out." (attributed to Tony Hoare)
0 Kudos
Message 4 of 4
(2,433 Views)