LabVIEW Idea Exchange

cancel
Showing results for 
Search instead for 
Did you mean: 
Darin.K

Negative Values in Index Array or Array Subset

Status: New

There are many times when I need to grab the last element or last n elements of an array.   This usually involves a call to Array Size and then a subtraction, costing me time and block diagram clutter.  Array reversal is essentially free, but again costs clutter and clicks.  For common array types I have personal VIs to do the job, but that is just a band-aid with the proliferation of data types that I use.  My idea would be to treat negative values input to the index array or array subset VIs (for example) as counting from the end of the array.  The last value would have index -1 and so on.  To get the last n values I would put -n into the Array subset VI and that's it.   For expanding the Array Index VI, my preference would be that it counts down (-2,-3,-4,...).

 

 

 NegativeArrayIndex.png

33 Comments
Knight of NI
I don't like the -1 idea that much either, or the counting backwards. A possible alternative is to have an option on the array functions to limit the index values that get wired to the bounds of the array. This would be selected via a right-click on the function, and would be OFF by default. For example, if the array has 10 elements, and you wire a value of 11 for the index number to Index Array, then the 10th element would be returned. If you want the last one in general, you can simply connect the +Inf constant to the index. This has the obvious side effect of not really allowing you to catch situations when you try to index something that's out of bounds.
altenbach
Knight of NI

> I must be missing something, but if you want to get the last elements of an array, why don't you use the delete from array primitive (see attached image)?

 

Delete from array might cause performance issues, because it seems that it creates another array copy, even if the upper output is not used. I haven't fully studies this, but it seems there is an array allocations as seen in the image. If you replace the "delete from array" with an "index array" in this same code, the buffer allocation disappears.

Message Edited by altenbach on 08-10-2009 12:08 PM
gb119
Member

I'm not sure that I see a problem with all the affected nodes having a "9.x mode" that is turned on ajutomatically when LabVIEW recompiles code from 2009 or earlier. Defining that Search 1D array will always give posotive numbers for found elements is no big deal - relying on the not found out put giving a default value when wired as an index to an array is not particularly elegant code.

As for the off by one compared to indexing forwards in the array - I don't really see it - think of your array as a circular buffer, then the zero index marks an arbitary start and -1 naturally means the element before the start point in the circle. The only question becomes what to do if the index is bigger in magnitude than the array do you keep going round the circle ? (Probably not but...)

--
Gavin Burnell
Condensed Matter Physics Group, University of Leeds, UK
http://www.stoner.leeds.ac.uk/
Darin.K
Trusted Enthusiast
I agree with the circle analogy, but I don't think I'd like to continue around in a circle, if you exceed the length in the negative direction you can return the default value just like it does currently for positive values.  I am not necessarily fond of that behavior, but I don't really see a good alternative.  There is plenty of room on the output side of the index array vi (and others) so maybe it could also output an error code or similar flag that the index was out of range.  That is perhaps an idea for a different day.
Mellroth
Member

I'll put my wote on the no-side mainly because of the backward compatibility issues.

I rather have NI work on the inline optimization so that we can always use reverse array before indexing without any mem-copy.

 

tstahl
Member

This would break a lot of code for me; is -1 a not found or the last index?

 

I occasionally get a default element from an array using an index of -1.  This has been useful for me and I've not had much issue with performing a sizeof and then subtracting the current loop index when processing in reverse, or reversing the array then using a for loop.

 

Rob_Calhoun
Member

The existing behavior should not be changed. 

 

I rely on the fact that a negative index returns the default value. This is a natural construction to use after a failed search, and it mirrors the behavior of out-of-bounds indexing in the other direction (i.e. index 0 of a length 0 array.)

 

I wouldn't be opposed to a brand new "Index Array From End" function, but honestly plucking the last element using Delete From Array has addressed 90% of my needs, and I've not been bothered by handling the other 10% with Reverse Array or doing the math.

TCPlomp
Trusted Enthusiast

The OpenG toolkit has a function called slice 1D array.

 

 

This will allow you to have a string-driven array selection set.

For instance the last five element can be retrieved with the '-5:' string.

I believe the subset string originates from Python.

 

Ton

Free Code Capture Tool! Version 2.1.3 with comments, web-upload, back-save and snippets!
Nederlandse LabVIEW user groep www.lvug.nl
My LabVIEW Ideas

LabVIEW, programming like it should be!
fomine
Member

Why don't add a bool terminal "From the end?" defaulting to False.

This wy the old code is inaffected, no mess with shifts by one, and the terminal will easily get the value from "Less than zero?" or similar...

-fomine
_________________________________
You can do greater things with LabVIEW!
AristosQueue (NI)
NI Employee (retired)
Just drop Reverse Array and then use Index Array as normal.