LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

how to remove zero from array quickly

Dear all: I got a problem for how to remove zero from array. I have more than a million data, each containing 2 1-D arrays, one for dividend and another for divisor. I need to calculate an array of quotient for each data. The problem is there are several elements of zero value randomly existing among the divisor arrays in these data. I got 2 choices: 1, remove these elements. For example, if the 1st and 4th elements of the divisor array are zero, these elements are removed from both divisor and dividend arrays, so that the quotient array has 2 less elements than dividend and divisor arrays. 2, give zero value to these elements in the quotient array.
 
I used a for loop to search each element and a case structure to tell if the element is zero, but it is too slow. As you see, I need to deal with more than 1 million data and this work will be applied to deal with more data in a very short time. Each array has about 400 to 500 elements, and I have only 0.5 ms or less to obtain the quotient array.
 
I wish someone can help me figure out a faster way to overcome this challenge. Thank you all in advance.
0 Kudos
Message 1 of 11
(8,509 Views)
You should post your code so that we have something to play with.

___________________
Try to take over the world!
0 Kudos
Message 2 of 11
(8,499 Views)
You have several options.
  • Option 3: Dividing by zero will give you NaN, you might just leave it at that. (upper code)
  • I would not remove the elements, because this will change the array size and require expensive memory allocations.

You could also do the division in a FOR loop and deal with the result as it happens. (lower code).

Message Edited by altenbach on 05-31-2007 12:23 PM

0 Kudos
Message 3 of 11
(8,500 Views)
Dear all: Thank you for so fast reply! I really appreciate for your quick response. The attached code is a simplified example I am doing, which is not completely correct. The problem is, I need to calculate the mean and standard deviation for all of the arrays. Thus, I must avoid any element containing NaN. Because of the same request, any element with zero value in the divisor array is void and should be removed. My second option is considered only when I have no way to do the first option quickly. So, do you all have better ideas? Thank you again.
0 Kudos
Message 4 of 11
(8,488 Views)

As Altenbach has shown, it would be much quicker to operate on the entire array once.

Attached is an example which does remove the NaN elements, but depending on various variables, there might be faster solutions (for example, if there is a small number of NaNs, it might be faster to delete them from the array, etc.).


___________________
Try to take over the world!
Message 5 of 11
(8,481 Views)
How about this?
Message 6 of 11
(8,479 Views)

And just to toss in another idea...

Can you check for them when the array is being created and just not include them at that time?

Please disregard this post if not applicable.

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 11
(8,472 Views)
tst,
 
One important correction in your code: The output of "boolean to 0,1" is I16 (!) so you definitely need to insert a "to I32" before summing or you'll quickly run out of numbers for any larger array size. 🙂
0 Kudos
Message 8 of 11
(8,466 Views)
Here's what I would do if you really want to remove the bad elements, shrinking the array in the process.
  • re-use one of the input arrays and index it at [i] for the division.
  • Insert the current result and increment the insert point for each good calculation.
  • trim the final array to the correct size at the end.

The other case is "0" and has the two shift register data wires simply wired across.

Message Edited by altenbach on 05-31-2007 01:41 PM

Download All
Message 9 of 11
(8,466 Views)
Could you bundle the data into a cluster with the divisor as the first item in the cluster. Sort the cluster array based on the first item in the cluster. Then split the sorted cluster array at the first non zero element.

Message Edited by unclebump on 05-31-2007 04:47 PM

Message 10 of 11
(8,449 Views)