LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Find the closest number from 2D array

Solved!
Go to solution

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

leandrofeder_0-1716412748459.png

 

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

0 Kudos
Message 1 of 11
(728 Views)
Solution
Accepted by topic author leandrofeder

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

 

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

Message 2 of 11
(699 Views)

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):

 

 

altenbach_0-1716436068532.png

 

0 Kudos
Message 3 of 11
(685 Views)

@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:

1DvsBS.png

Look like "brute force" linear search for me:

Screenshot 2024-05-23 08.23.10.png

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

Message 4 of 11
(672 Views)

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)

0 Kudos
Message 5 of 11
(649 Views)

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

0 Kudos
Message 6 of 11
(647 Views)

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

0 Kudos
Message 7 of 11
(629 Views)

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

 

QUOTE: "Interpolates points in a 1D array that represents a 2D non-descending graph."

0 Kudos
Message 8 of 11
(588 Views)

@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!)

 

altenbach_0-1716482960836.png

 

0 Kudos
Message 9 of 11
(560 Views)

@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!)

 

altenbach_0-1716482960836.png

 


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

0 Kudos
Message 10 of 11
(551 Views)