LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Fast way to find the number of non-zero terms in an array or image

Solved!
Go to solution

I am trying to use the Standard Deviation PtbyPt Vi.  It takes in a series of points, can be reset when beginning the calculation.  There is also an input for sample size.  I am calculating some statistics on an array...and would only  to include like the non-zero elements that are  in the sample. 

 

Is there a quick way to look at an array and find the number of non-zero elements?

 

Thanks.

Message 1 of 10
(6,862 Views)
Solution
Accepted by topic author Hummer1

Hi Hummer,

 

test the array for "unequal to 0", convert the resulting boolean array to numbers and sum up the array. Just three LabView primitives.

 

Be aware:

- for big arrays you need an additional step of converting the numbers array to I32 before summing up

- for big arrays this may lead to "out of memory" problems because of creation of data copies

Message Edited by GerdW on 10-23-2008 06:03 PM
Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
Message 2 of 10
(6,860 Views)

Well, now that I have tried to do this I am confused.  I guess I need a little help.

 

Do you have to step through the 2x2 array using indexing on and do element by element comparison before going on...or do you first do the not equal 0 to build an array of boolians...and then count the boolians by using a select in an nxn do loop....

 

A hint would be appreciated.

0 Kudos
Message 3 of 10
(6,835 Views)

Most LabVIEW primitives are polymorphic. This means they automatically adapt to the datatype wired to them. Wire a scalar, get a scalar. Wire an array, get an array. Try it.

 

To learn more about LabVIEW it is recommended that you go through the tutorial(s) and look over the material in the NI Developer Zone's Learning Center which provides links to other materials and other tutorials. You can also take the online courses for free.

0 Kudos
Message 4 of 10
(6,829 Views)

One example of the polymorphism smercurio mentioned...

 

 

Message 5 of 10
(6,824 Views)

GerdW wrote:

- for big arrays this may lead to "out of memory" problems because of creation of data copies


Here's a quick rundown on memory allocations based on the algorithm.

 

(case 1&3)

If you do the "!=0, ?1:0, I32, sum" shuffle, each step needs to allocate a new array, so if the input array is DBL and uses N amount of memory, we need 7N/8 more memory.

(Total of 15N/8, or 13N/8 if we don't need the I32, but in this case the array are too small to worry about anything ;))

The nice thing is that it works for any array dimensionality (1D, 2D, 5D, etc...)

 

(Case 2&5)

If you do the summing in a shift register, No additional array memory is needed. Unfortunately, the code needs to be changed by adding an extra FOR loop for each dimension (see case 5 for 2D inputs).

 

(Case 4)

If we reshape the N-D array to a 1D array, we need to create a second copy, so the memory usage is about twice. This also works for any dimension.

 

In real life, and if memory is a potential issue, you should always do some benchmarks.

Often, the issues are more complex, so this is just a quick summary.

 

Message Edited by altenbach on 10-23-2008 11:59 AM
Message 6 of 10
(6,804 Views)

Case 5 is what I was trying to noodle out.  Although case 3 is certainly simple.

 

Thanks a million to all...

Smiley Happy

0 Kudos
Message 7 of 10
(6,789 Views)

Hi 

I am trying to add the number of ones . I did exactly as you said but still it is not working?

 

Could you please navigate me?

 

BR

Fatemeh

0 Kudos
Message 8 of 10
(5,778 Views)

Hi 

 

I did exactly as you said but it is not working. I want to add the number of ones in array. 

 

Could you please help me . 

 

0 Kudos
Message 9 of 10
(5,820 Views)

Duplicate post, continue here!

 

Don't hijack old threads (unrelated to your post)!

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 10 of 10
(5,801 Views)