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
Darin.K
Trusted Enthusiast

> Consider putting an "inversion circle" like the ones used in the Compound Arithmetic and some Boolean functions on the index input

 

Assuming they do this as well:

 

http://forums.ni.com/t5/LabVIEW-Idea-Exchange/Enlarge-the-Invert-Circles/idi-p/1023748

dnoble79
Member

My suggestion is to add a item to the context menu to select between Absolute and Modulus (or Modulo) Indexing. For example:Array Modulus Indexing.pngThe default is Absolute so existing functionality is unchanged. Modulus Indexing can be selected when needed. This way -1 gives the last element in the array, -2 the second last and so on till the wraps around. The same could be added to other array functions.

These functions would be great for circular buffers too!

 

AristosQueue (NI)
NI Employee (retired)

I uploaded a malleable VI that does this:

The document has been Archived -- no longer available

I've played with this functionality a bit (since I've had malleable VIs longer than most of you). Honestly, the negative indicies just feel error prone. After using this in a couple places, I think I would rather have a function "Reverse Index Array" in the palettes so the last element is index 0. The "off by one" feeling is really hard to think about.

 

This node isn't growable, so it doesn't give the full Index Array experience, but I figured some people in this discussion might want to play with it.