LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Removing a list of indices from an array

Now the question would be if we can improve things even more using the new In Place Element Structure of LabVIEW 8.5.
 
(See also http://forums.ni.com/ni/board/message?board.id=170&message.id=265705 for some discussion of this new feature)
Message 21 of 40
(3,753 Views)
We have been using code similar to Christian's suggestions for a while.  I can confirm that it is much faster than the OpenG version (Sorry, Jim, for not getting around to submitting it).

In our version, we generate a list of booleans for the array elements to keep (of course it's easy to throw a NOT in there), and we use the same technique of template code to actually do the array modification. We are always doing this on different cluster datatypes so it is not feasable or useful to make it into a polymorphic VI.

It is very easy to generate the array of booleans, in whatever FOR loop is making the decision on which items to keep (or delete) and then you avoid the need for validating the input.

Jason Dunham

BTW, how do I in-line the image?

Message Edited by jdunham on 08-30-2007 12:33 PM

0 Kudos
Message 22 of 40
(3,750 Views)
Jason,
 
Could you mold your tecnique into something that is input compatible with the above VIs so I can do some benchmarks. Of course we need to include the time to make the boolean array. 
 
 
For special cases we can even to the tagging in the original array if we know that a certain element cannot exist. (e.g. NaN, -Inf, +Inf for DBL, -1 if all values are known to be positive, etc.).
We simply do the tagging in place, then remove all tagged elements. This seems to be the fastest yet. As you can see from the buffer allocation display, everything is "in place"!
 
 
(To inline an image, attach it first, then edit the post and point to the image link of the attachment).

Message Edited by altenbach on 08-30-2007 11:15 AM

Message 23 of 40
(3,736 Views)
Christian:

I attached some VIs for you, including the indexer, the wrapper you requested, a solution optimized for huge arrays, and a different example which shows a more normal use case for array filtering.  I think for the stated problem of huge arrays, a lot depends on the size of the deletion list.  If is usually small, like < 1% of the total size, it may be better to iterate over the deletion list rather than iterating over the input array. I would love to see a benchmark, because I would think it is not necessary for LabVIEW to copy the array just to delete an element.



For more typical cases with the input array containing fewer than maybe 100000 elements, I would bet it is more intensive and certainly more programming work to generate the index list than to use the boolean array.  So it's kind of unfair to benchmark this solution with the overhead of generating the boolean array unless you also benchmark the creation of the index list for other solutions.  I believe the index list is only beneficial for the original poster's less common question of deleting a few items from an enormous array. 

Here is a picture of how I normally use the filter:


I guess I am trying to say that once I started using the array indexer, I don't know how I ever got on without it  (Good-bye build array in a case structure in a for loop!).  I also have to give the credit to my amazing co-worker Rob Calhoun who took my original version and optimized the heck out of it.

Jason



Message Edited by jdunham on 08-30-2007 03:01 PM

Message 24 of 40
(3,707 Views)
Thanks Jason. Maybe I can study this tonight.

@jdunham wrote:
I would love to see a benchmark, because I would think it is not necessary for LabVIEW to copy the array just to delete an element.

As far as I know, arrays must occupy contiguous memory locations. So if you have an array with one million points and delete element #2, elements #3 and above need all to be moved down by one slot. Even if you don't allocate a new array for the result, all higher elements must be moved for each deletion operation so if you delete 100000 elements using "delete from array" in a loop, the last element in the array needs to be moved down 100000 different times. That's a lot of work! In my original code, each element is only moved exactly once.
0 Kudos
Message 25 of 40
(3,693 Views)


@altenbach wrote:
As far as I know, arrays must occupy contiguous memory locations. So if you have an array with one million points and delete element #2, elements #3 and above need all to be moved down by one slot.

Yes, but it's really, really fast.  Just think of how fast LV can transpose a huge 2D array (it's fast).


Even if you don't allocate a new array for the result, all higher elements must be moved for each deletion operation so if you delete 100000 elements using "delete from array" in a loop, the last element in the array needs to be moved down 100000 different times. That's a lot of work! In my original code, each element is only moved exactly once.

I would agree that as you get to a large deletion list, my solution may lose its advantage.  But your method still runs a bunch of LabVIEW nodes for each element of your huge array, that is guaranteed to be somewhat slow, since you are taxing the LabVIEW scheduler.  My other solution (the one with the array of booleans) has the same problem, but for non-huge applications it is both fast and simple to code.

I hope you get a chance to benchmark them and tell us where the break-even point is.

Oh, and there was a bug in my original solution regarding the removing of unique elements, so I attached a new version

Jason

Message Edited by jdunham on 08-30-2007 03:33 PM

Message Edited by jdunham on 08-30-2007 03:34 PM

Message Edited by jdunham on 08-30-2007 03:35 PM

0 Kudos
Message 26 of 40
(3,689 Views)
Well now that I'm thinking more about all those moves (array element deletions), I guess it makes sense that every shift is an O(n) operation, and they sure do add up.  I did a crude benchmark, and the break-even point for deletion from a 10M element array is about 5 elements.  If there are more than 5 elements to be deleted, then the penalty for repeated array delete commands is greater than the savings I get by not iterating labview code for each element in the array.

Jason
0 Kudos
Message 27 of 40
(3,656 Views)
Here's an example using the In-Place structure. Seems to work, although I must admit I'm not 100% why! Anyone want to profile it against Altenbach's other method?

I'll attach the pic for those of you without LV85.


Message Edited by Jarrod S. on 08-30-2007 05:19 PM

Jarrod S.
National Instruments
Download All
Message 28 of 40
(3,657 Views)

I'm interested in the compare of Jarods new way and Christian old way.

I would guess a tie if Christian did not win.

Ben

Retired Senior Automation Systems Architect with Data Science Automation LabVIEW Champion Knight of NI and Prepper LinkedIn Profile YouTube Channel
Message 29 of 40
(3,656 Views)
My initial profile has Christian winning by about 10% in various tests. I must be doing an extra scalar copy or something. Still, looks neat!
Jarrod S.
National Instruments
Message 30 of 40
(3,654 Views)