# LabVIEW

cancel
Showing results for
Did you mean:

Solved!
Go to solution

## Find the closest number from 2D array

Hello guys,

I'm trying to get the closest or equal value to an specific value that's in a 2D array

I also need to find the line number where this value is

How can i find the value and the index of the row of this value?

Message 1 of 11
(351 Views)
Solution
Accepted by topic author leandrofeder

## Re: Find the closest number from 2D array

"Array Max & Min" can find the smallest difference.

Also, most of these math nodes work with N-dim arrays.

Message 2 of 11
(322 Views)

## Re: Find the closest number from 2D array

Form your default values, it seems obvious that you are only interested in the first column, which is sorted.

This you can use "threshold array" as follows. (This is extremely efficient because it uses a binary search):

Message 3 of 11
(308 Views)

## Re: Find the closest number from 2D array

@altenbach wrote:

This you can use "threshold array" as follows. (This is extremely efficient because it uses a binary search):

Slightly off-topic, but to be honest — I don't think it uses binary search under the hood of the Thr 1D:

Look like "brute force" linear search for me:

That was a reason why we replaced Threshold 1D with BS and got some performance improvements in Interpolate Pixel Topic.

Message 4 of 11
(295 Views)

## Re: Find the closest number from 2D array

I guess you are right. The solutions are not equivalent because non-descending can mean that there are duplicates. For example if we have an array [1,2,2,2,2,2,2,2,2,2,2,2,2,2,3] and look for 2, the result would be very different. Still, that can be fixed with a bit more code. 😄

(On another side note, your binary search looks way too orange... 😄 I don't have LabVIEW on my current computer so I cannot test at the moment)

Message 5 of 11
(272 Views)

## Re: Find the closest number from 2D array

@Andrey_Dmitriev wrote:

@altenbach wrote:

This you can use "threshold array" as follows. (This is extremely efficient because it uses a binary search):

Slightly off-topic, but to be honest — I don't think it uses binary search under the hood of the Thr 1D:

I think you're right, because the 1D array isn't guaranteed (or required IIRC) to be ordered. Interpolate 1D Array will return the 1st found index >= "fractional index of x".

Now there's a 'sorted array' thing going on, a Interpolate Sorted 1D Array makes even more sense.

EDIT (AGAIN): That crossed...

Message 6 of 11
(270 Views)

## Re: Find the closest number from 2D array

@altenbach wrote:

your binary search looks way too orange... 😄

For me it looks more "sunny" rather than a cold blue color.

Originally, it was mostly based on integer arithmetic, but the message above I've seen Input Array and a search item that are both doubles, so I quickly changed it accordingly to avoid coercion dots (and didn't check for special cases). The only algorithm's time complexity was in scope.

It could be improved, for sure, no doubt.

Message 7 of 11
(252 Views)

## Re: Find the closest number from 2D array

wiebe@CARYA wrote:
I think you're right, because the 1D array isn't guaranteed (or required IIRC) to be ordered.

The help says "non-descending"., but I don't understand the 2D part: Must be some math lingo.... 😄

Message 8 of 11
(211 Views)

## Re: Find the closest number from 2D array

@Andrey_Dmitriev wrote:

@altenbach wrote:

your binary search looks way too orange... 😄

Originally, it was mostly based on integer arithmetic, but the message above I've seen Input Array and a search item that are both doubles, so I quickly changed it accordingly to avoid coercion dots (and didn't check for special cases).

Indices and data never mix (until after the while loop!)

Message 9 of 11
(183 Views)

## Re: Find the closest number from 2D array

@altenbach wrote:

@Andrey_Dmitriev wrote:

@altenbach wrote:

your binary search looks way too orange... 😄

Originally, it was mostly based on integer arithmetic, but the message above I've seen Input Array and a search item that are both doubles, so I quickly changed it accordingly to avoid coercion dots (and didn't check for special cases).

Indices and data never mix (until after the while loop!)

Ah, constants are occasionally touched. It happens sometimes, no problem. Thank you for the notice.

Message 10 of 11
(174 Views)