LabVIEW

cancel
Showing results for
Did you mean:

Solved!
Go to solution

finding nearest value in array

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!

Message 1 of 11
(4,701 Views)

Re: finding nearest value in array

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

/Y

"Only dead fish swim downstream" - "My life for Kudos!" - "Dumb people repeat old mistakes - smart ones create new ones."
Message 2 of 11
(4,689 Views)

Re: finding nearest value in array

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

Subscribe to the Test Automation user group: UK Test Automation Group
Message 3 of 11
(4,687 Views)

Re: finding nearest value in array

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

"Only dead fish swim downstream" - "My life for Kudos!" - "Dumb people repeat old mistakes - smart ones create new ones."
Message 4 of 11
(4,663 Views)

Re: finding nearest value in array

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

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

Message 5 of 11
(4,662 Views)

Re: finding nearest value in array

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

Thanks!

Message 6 of 11
(4,613 Views)

Re: finding nearest value in array

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
Message 7 of 11
(4,600 Views)

Re: finding nearest value in array

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

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
Message 8 of 11
(4,595 Views)
Solution
Accepted by topic author rainy01

Re: finding nearest value in array

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
(4,587 Views)

Re: finding nearest value in array

@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

Message 10 of 11
(4,577 Views)