LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Remove elements from an array

Hello!

Another question: We have an array with different numbers. If there is a zero (can be moore than one and they could be placed on different places in the array). Is there a way to remove these zeros from the array? We have tried with a combination of case, delete from array, search 1D array. Any tip? Thank you and best regards.
0 Kudos
Message 1 of 16
(7,548 Views)
hi,
See the attached image it may help you.
cheers
vicky
0 Kudos
Message 2 of 16
(7,547 Views)
Hi

Are the numbers of datatype double?

If yes this might cause problems, because somewhere far behind the dot may be some value. Comparing a double value with zero is not good.

Try it with a level-value (remove all values below value x).

Hope this helps.

Thomas
Using LV8.0
--------------------------------------------------------------------
Don't be afraid to rate a good answer... 😉
--------------------------------------------------------------------
0 Kudos
Message 3 of 16
(7,263 Views)
Hello!

No the numbers are in I32... but our problem is that we are not so good in using shiftregisters and indexing arrays... but now we know that it is useful 🙂 Thank you very much for your help vicky!
0 Kudos
Message 4 of 16
(7,213 Views)
A slightly better way would be the one in the attached image. It goes through the array and removes the zeros instead of building a new array without the zeros. If you have a very large dataset, this can be a better use of memory.
Message 5 of 16
(7,518 Views)
(Actually, Bob, your solution is probably not very efficient, because every "remove from array" operation is a resizing operation, having to shift ALL upstream elements down by one. (It might be OK if there are only very few zeroes.) The search for zeroes is a cute idea, but I doubt that it can search faster than the loops in my example. One way or another, the code MUST look at all elements ;).)

Personally, I would do something like in the attached image (upper example on image). It is simple, easy to read, and efficient.

It might be slighly more optimal (you need to test this!) for very large arrays to re-use the array and maintaining the new insert point in another shift register. Then, after the loop, you reshape (= truncate in this case) to the new lenght in one step. (lower example on image).

Message Edited by altenbach on 03-17-2005 08:11 AM

Message 6 of 16
(7,509 Views)
Hi Altenbach and Bob,

If the array is large and the number of "0"s is small, the code illustrated in the attached could perform well.

It is sorta a combo of the two approaches that you both posted.

It uses an initial While loop to find where the zeros are and then only removes those elements that need tossed.

Ben
Retired Senior Automation Systems Architect with Data Science Automation LabVIEW Champion Knight of NI and Prepper LinkedIn Profile YouTube Channel
0 Kudos
Message 7 of 16
(7,459 Views)
This is the code as LV 7.1.

Ben
Retired Senior Automation Systems Architect with Data Science Automation LabVIEW Champion Knight of NI and Prepper LinkedIn Profile YouTube Channel
0 Kudos
Message 8 of 16
(7,459 Views)
OK, I took the liberty to stage a competition between the four code suggestions: Bob, Ben, Altenbach1 (upper code in image), Altenbach2, lower code in image.

The attached table shows some results with various array sizes and various fractions of zeroes.

Some comments:
The versions by Bob and Ben are only suitable if there are very few zeroes. Both use "delete from array", a very expensive function because for every zero found, all higher elements must be moved down one slot. Ben is faster despite the fact that he uses 2 loops and reverses the array. Bobs version can beat Ben by keeping the current index in a shift register, then start searching from there. Right now, every search starts with element zero so large portions, so e.g. the first element is searched as many times as there are zeroes in the entire array. Both depend strongly on the number of zeroes in the array. If there are no zeroes, they are very fast. They slow dramatically with the numbers of zeroes present.

My simple version (upper panel, altenbach1) is pretty fast, but around inputs of 0.5-1MB it slows down dramatically. I have not explored the details, but most likely LabVIEW "underguesstimates" the final array size and must reallocate the output during the loop. Maybe there is some other reason (e.g. cache issues, etc.).

As expected, my second version (lower panel, altenbach2) is the only consistent performer. The times are independent of the fraction of zeroes and the execution time seems linear with input array size (up to 20MB tested!)
Message 9 of 16
(7,420 Views)
Thank you!

That is why I posted the code.

As I suspected, good with low "0" count only.

Ben
Retired Senior Automation Systems Architect with Data Science Automation LabVIEW Champion Knight of NI and Prepper LinkedIn Profile YouTube Channel
0 Kudos
Message 10 of 16
(7,416 Views)