LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Search array for closest value

Solved!
Go to solution

Im wondering if its possible to search a 1D array for a value, if not found, then to find the closest match. Im using "double" data type and i need atleast a 4 decimal point accuracy in the search.

For now, im rounding the decimal values to the closest integers, but it is not efficient at all. Im getting a lot of undesired spikes in the plot. I need to be able to search decimal points. I found this function "Approx Equals.VI" from a previous thread. Even that uses the concept of rounding the digits.

I have attached 3 VIs. The "0.8 & 0.4.VI" is the one i made, just by rounding the decimals to closest digits.

The "Approx Equals" is the one i found online.

The 3rd one is the one i was trying to mess with so that i can find a closest match.

 

Is there a trick to get around this?

 

 

 

Thank you,

Eureka

 

0 Kudos
Message 1 of 10
(8,030 Views)
In this situation I usually subtract the target value from the array, take the absolute value then find the index of the minimum value. You can then decide if the value is close enough. In Range & Coerce is also useful if you want to find multiple values.
Message 2 of 10
(8,029 Views)

When I need to do this, I use Threshold 1D Array and Interpolate 1D Array.  Usually, I am using them together, but in your case, you may just need the first one.  You may need to sort the array for your case.  The output of the Threshold can be rounded up or down to find the closest point.  Since LV does the interpolation, doing a "normal" rounding will get you to the closest index.

Message 3 of 10
(8,023 Views)

Matthew,

 

Thats a beautiful idea, i did that. Im not getting the desired output. Guess im missing something fundamental...Im able to read the index, but im missing something...thanks for the idea, it was simple and efficient.

0 Kudos
Message 4 of 10
(7,988 Views)

Here's a simple example.

 

ScreenHunter_01 Nov. 02 21.13.jpg

 

ScreenHunter_02 Nov. 02 21.15.jpg

Message 5 of 10
(7,977 Views)

If your data is more complicated, post a copy of the dataset.

Message 6 of 10
(7,973 Views)

Matthew,

My data is a bit complicated.

As a first step, i need to separate the data according to the "Loop Number". I did that part and i have the index at which each and every loop begins and ends.

Next, for each and every loop, i need to search for "Step Type #3" and find the index of that location. Im able to do that as well.

Now, for the index value obtained form the "search for 3" function, i need to move exactly one index up(decrement by 1) and get the "Voltage" at that index. Yes, im getting the value, thats not a problem.

Let us say the Voltage value at that index is X, i need to find the index at which the "Voltage" value drops to "0.9*X" & "0.7*X". (Im trying to find the slope of the array). And, i need to read the "Test Time" column for the 2 index values so that i can find the (change in voltage)/(change in time) ratio. This is where im having a problem, if i round the values to the closest integers, it works, but the values are not even close to accurate.

I used your logic, i.e., i subtract "0.9*X" from the entire array, and read that index as the closest match. And i give that in a for loop so that it performs the same for every "Loop Number". Im not able to find the correct index values here.

Download All
0 Kudos
Message 7 of 10
(7,955 Views)
Solution
Accepted by topic author Eureka

This VI uses the first step type 3 in your data.  Since you said you can get the data, you can drop this into the VI.

 

At the end where I calculate slope, you can use Linear Fit if you have Full or Professional LabVIEW.

 

ScreenHunter_02 Nov. 03 19.21.jpg

 

ScreenHunter_03 Nov. 03 19.23.jpg

Message 8 of 10
(7,941 Views)

Matthew,

 

That was a brilliant idea, it worked perfectly after adding an index function and an absolute sign function. Wish i could give 1000 Kudos. Awesome...

 

 

 

Thank you,

Eureka

0 Kudos
Message 9 of 10
(7,922 Views)

@Eureka wrote:

Matthew,

 

That was a brilliant idea, it worked perfectly after adding an index function and an absolute sign function. Wish i could give 1000 Kudos. Awesome...

 

 

 

Thank you,

Eureka


 

You Can!

 

Start here.

 

Stop before your mouse hand starts to ache.

 

Ben

 

Retired Senior Automation Systems Architect with Data Science Automation LabVIEW Champion Knight of NI and Prepper LinkedIn Profile YouTube Channel
Message 10 of 10
(7,913 Views)