LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

numpy searchsorted implentation/labview equivalent?

So as the title suggests I am wondering if there exists an equivalent to numpys searchsorted function which basically takes in an array and an element as input and outputs the index where it should be inserted to maintain order. So for instance if I have the array [1,2,3,4] and element 5 the output would be 4 since it has to be added to the end to maintain order. Does there exist a labview equivalent of this or do I have to implement it myself? And if yes what would be the easiest way to do it?

0 Kudos
Message 1 of 8
(183 Views)

Hi Asasa,

 

In LabVIEW 2020 (and above) we have VIs for sorted arrays :

 

PinguX_0-1737036910024.png

 

Message 2 of 8
(171 Views)

Hi,

 

Oh ok, I'm running LV2018 so I suppose it would have to be implemented then...

0 Kudos
Message 3 of 8
(161 Views)

I'm on LV2019 so I can't check, but I know the other pink VIs (.vims) in my palettes are NOT actually primitives and can be opened to see the block diagram.  If someone (Hi PinguX 😉) were to post a screenshot or snippet, the OP could probably reproduce it...

LabVIEW Pro Dev & Measurement Studio Pro (VS Pro) 2019
0 Kudos
Message 4 of 8
(143 Views)

You don't need those functions. Use "Threshold 1D array". That returns the fractional index where your new element would go.

 

BertMcMahan_0-1737067042397.png

 

However, note that the function returns a fractional index. To get the location of the NEW thing, you'll want to do some shenanigans with the index. This is because "Threshold 1D Array" returns a max value of the index of the current last element, which means it can never tell you that something belongs AFTER the last element.. So, in your case, Threshold 1D Array will return a value of 3.

 

Example_VI.png

 

So, here I check if the new value is greater than the largest value in the array. To get the largest value in the array, you can use Array Max & Min, but that function looks through the whole array to find the biggest. Since this is, by definition, already sorted, you can just get the last element. (Delete from Array with no inputs will give you this.)

 

So, if your new element is greater than the last element, you put it at the end (at the array's length). Otherwise, put the new element at the rounded-up result of Threshold 1D Array.

 

Basically, it sounds like the Python function you're emulating functions exactly the same as Threshold 1D Array, with the exception that Threshold 1D Array has an upper limit of the last element in the array.

Message 5 of 8
(100 Views)

 

If the array is floating point and guaranteed non-descending, one option would to to use threshold array to find the index to insert. I am pretty sure it does an efficient binary search, dramatically outperforming a linear search for big arrays.

 

For some additional ideas you might also want to look at my 2019 presentation, slide ~26. Just adapt it to your datatype  It could probably be turned into a vim.

 

 

altenbach_0-1737072763112.png

 

Message 6 of 8
(87 Views)

@PinguX wrote:

Hi Asasa,

 

In LabVIEW 2020 (and above) we have VIs for sorted arrays :

 


in labview 20.0 i don't have those "insert into sorted array functions" - but in labview 2024 Q1, they are are there.

0 Kudos
Message 7 of 8
(58 Views)

@alexderjuengere wrote:

@PinguX wrote:

In LabVIEW 2020 (and above) we have VIs for sorted arrays :


in labview 20.0 i don't have those "insert into sorted array functions" - but in labview 2024 Q1, they are are there.


Interesting. I have not looked there (only have 2024 on a VM for now). Insert into sorted does not depend on duplicate elements. However if there are duplicate elements, a binary search might not find the first occurrence in a stretch of duplicates and requires slightly more code if that is a requirement.

 

(For history, there is also this idea from 2016 😄 )

0 Kudos
Message 8 of 8
(22 Views)