LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

finding nearest value in array

Solved!
Go to solution

Hello everyone,

 

I need to find the values in the row "C" which is equal or nearest to the values "A" in 2D Array, and then I want to show the values of "B" corresponding to the values of "C" in 2D array.

 

How do i solve it?

 

Thanks!

 

 arrays.png

 

0 Kudos
Message 1 of 11
(5,868 Views)

How do you plan to compare a 2 unsorted arrays of different dimensions? What's the result supposed to be?

/Y

G# - Award winning reference based OOP for LV, for free! - Qestit VIPM GitHub

Qestit Systems
Certified-LabVIEW-Developer
0 Kudos
Message 2 of 11
(5,856 Views)

Also you need to define a tolerance or how do you quantify "nearest value"?


CLA CTAChampionI'm attending the GLA Summit!
Subscribe to the Test Automation user group: UK Test Automation Group
0 Kudos
Message 3 of 11
(5,854 Views)

You could ofc. transform the 2D array to a 1D-array and sort it, then do a compare until you find a value bigger than your reference (and compare to the last one to see which is closer).

/Y

G# - Award winning reference based OOP for LV, for free! - Qestit VIPM GitHub

Qestit Systems
Certified-LabVIEW-Developer
0 Kudos
Message 4 of 11
(5,830 Views)

??? It became clear that your question isn't clear only after trying to answer it.  If I'm close, my questions match the other responses.  What is the result supposed to be?  I got "8", but "10" seems a good answer, too.  My first thought was that once I find a match (array minimum) I should look for another one that is the same minimum, but then we get to comparing floats for equality and that leads to needing a tolerance.

 

nearestvalue.png

Jim
You're entirely bonkers. But I'll tell you a secret. All the best people are. ~ Alice
For he does not know what will happen; So who can tell him when it will occur? Eccl. 8:7

0 Kudos
Message 5 of 11
(5,829 Views)

Hi all,

Thanks for all of your reply. I am so sorry that my question was not clear, and I just found that the values in 2d array was not correct, so i attached my new vi.

There are 40 elements in 2d array A (5x8). What I'd like to do is I want to compare each values in 2D array A against the values in row C to find the nearest or equal values of each elements in row C, and then i want to get the values (0,1,2,.. 11) in row B corresponding to different values in row C ,and show them in the 2D array indicator. So it is like row C will work as a translation vi for A to find the values from row B, and show in 2D array indicator(5x8).

 

For example: 1.0699 nearest value will be 1.068765 in row C and 11 in row B

                    0.9967 nearest value will be 1 in row C and 0 in row B

 

near.png

Thanks!

0 Kudos
Message 6 of 11
(5,780 Views)

Hi rainy,

 

why do I have the feeling of duplicate threads here? Apart from used labels the task is the same...

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 7 of 11
(5,767 Views)

This seems eerily similar to this thread:http://forums.ni.com/t5/LabVIEW/Round-up-to-nearest-decimal-point/td-p/2428736


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 8 of 11
(5,762 Views)
Solution
Accepted by rainy01

0.9967 nearest value is 0.993807 in row Cand 8 in Row B, not 1. Differences are 2.893E-3 and 3.300E-3.

 

Subtract Each value in A from the array C. Take the absolute value of the differences. Find the index of the minimum using Array Max & Min. Index the value from B. Using autoindexing, create output array.

 

Lynn

Message 9 of 11
(5,754 Views)

@johnsold wrote:

0.9967 nearest value is 0.993807 in row Cand 8 in Row B, not 1. Differences are 2.893E-3 and 3.300E-3.


I was going to post a snippet and the comment that I disagreed with the example, but you've given the answer so a snippet would be superfluous.

Jim
You're entirely bonkers. But I'll tell you a secret. All the best people are. ~ Alice
For he does not know what will happen; So who can tell him when it will occur? Eccl. 8:7

0 Kudos
Message 10 of 11
(5,744 Views)