01-16-2025 07:55 AM
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?
01-16-2025 08:16 AM
Hi Asasa,
In LabVIEW 2020 (and above) we have VIs for sorted arrays :
01-16-2025 08:32 AM
Hi,
Oh ok, I'm running LV2018 so I suppose it would have to be implemented then...
01-16-2025 09:08 AM
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...
01-16-2025 05:00 PM
You don't need those functions. Use "Threshold 1D array". That returns the fractional index where your new element would go.
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.
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.
01-16-2025 06:13 PM - edited 01-16-2025 06:16 PM
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.
01-17-2025 03:37 AM
@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.
01-17-2025 10:38 AM
@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 😄 )