LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to eliminate or remove zeros from 1D array

Solved!
Go to solution

How to eliminate or remove zeros from 1D array. Let say I have 1D array having foolowing elements

 

"0 0 0 0 0 4 0 0 9 0 0 1 4 0 0 0 0 0 0 0 0 10 9 0 0"

 

So after removing or eliminating zeros it will become as follow

 

"4 9 1 4 10 9"

 

So can any body guide me how can I do that? See attached Image for details.

 

Thanks 

 

JK

 

array.jpg

0 Kudos
Message 1 of 13
(25,915 Views)
Solution
Accepted by josephkirmani

I am sure this post could help you.

-----

The best solution is the one you find it by yourself
0 Kudos
Message 2 of 13
(25,911 Views)
Solution
Accepted by josephkirmani

P@Anand wrote:

I am sure this post could help you.


No, that's a bad and inefficient example. 9 years ago we discussed that in detail, and this VI (posted here) contains two possible solutions. If the array is large, you should use the lower solution. (read the entire thread!)

 

If you have a newer LabVIEW version (2012+), you can also use a conditional indexing tunnel for an even simpler solution. (see image)

 

Message 3 of 13
(25,903 Views)

The simple solution I use is the OpenG Filter 1D Array function.  It is not as efficient as other methods, but does have other features, and it is already written.  For huge amounts of data I would do something else, but for simple data it works fine.

0 Kudos
Message 4 of 13
(25,882 Views)

thanks it is perfect.

 

0 Kudos
Message 5 of 13
(25,869 Views)

Here are 2 example .vi's I made based on 's removeZeroes.png in this discussion thread: 

 

  1. "DeleteZeros_1D.vi" deletes zeros from a 1D array. 

  2. "DeleteZeros_2D_to_1D.vi" deletes zeros from a 2D array into a 1D array output (this discussion combined with looking at the picture shown in an answer at http://stackoverflow.com/questions/8849567/how-to-flatten-reshape-to-1d-an-array-of-arbitrary-dimens...). 
Download All
Message 6 of 13
(25,246 Views)

@hchiam wrote:

Here are 2 example .vi's I made based on @altenbach's removeZeroes.png in this discussion thread: 


It seems pretty pointless to post those because no new useful information is given.

  • Currently, only the input is defined in the connector pane, making them useless as subVIs.
  • unlike my example using integers, an "=0", like any "equal" comparison, is potentially dangerous with DBLs.
  • Reshaping a 2D array to 1D before removing zeroes is pretty pointless because a 2D output cannot be recovered. A more interesting scenario would be to remove some columns or rows (e.g. that are all zeroes) from a 2D array.
  • You should also clean up the front panel, e.g. properly zero the upper left corner, make the controls/indicators nicely labeled, sized and arranged, and maybe even add scrollbars to the arrays.
0 Kudos
Message 7 of 13
(25,219 Views)

altenbach a écrit :

@hchiam wrote:

Here are 2 example .vi's I made based on @altenbach's removeZeroes.png in this discussion thread: 


It seems pretty pointless to post those because no new useful information is given.

  • Currently, only the input is defined in the connector pane, making them useless as subVIs.
  • unlike my example using integers, an "=0", like any "equal" comparison, is potentially dangerous with DBLs.
  • Reshaping a 2D array to 1D before removing zeroes is pretty pointless because a 2D output cannot be recovered. A more interesting scenario would be to remove some columns or rows (e.g. that are all zeroes) from a 2D array.
  • You should also clean up the front panel, e.g. properly zero the upper left corner, make the controls/indicators nicely labeled, sized and arranged, and maybe even add scrollbars to the arrays.

 

Thank you for those comments.

 

  • Now the input and output are defined (in the updated attachments) for use as example subVI's.  I also cleaned up a few visual details, but I leave the rest for others to cater to their specific uses if they don't want to use this as a subVI (or at least as-is), such as changing the icons or changing to integers instead of doubles.
  • I'm not sure about what you mean exactly with "=0" being "potentially dangerous with DBLs".  I'm guessing you had a certain case scenario in mind.  Although the subVI's seem to work fine within my larger program and situation, my situation may not generalize with regards to this point.
  • I reshaped from 2D to 1D, with a 1D output, because it could be helpful for things like when later processing just needs a 1D array.  For example, in my situation I had to remove a huge spike of unnecessary "0"'s from a previous subVI that were affecting the output display of a certain histogram.  So it turned out a 1D array output was helpful, and you never know what problems come up, so I included the 2nd example.

 

Hopefully if someone is looking for this, they can just use or play with an example subVI (granted that they know how to plug things in for their context).  I was hoping to post a subVI people could put to direct use.

Download All
0 Kudos
Message 8 of 13
(25,182 Views)

hchiam wrote:
  • I'm not sure about what you mean exactly with "=0" being "potentially dangerous with DBLs".  I'm guessing you had a certain case scenario in mind.  Although the subVI's seem to work fine within my larger program and situation, my situation may not generalize with regards to this point.

He was refering to the fact that for a double to be equal to another double, 64 bits that represent the double need to be equal to another 64 bits.  And with fractional precision this can be a bit difficult.  Is 0.0 equal to 0.0000000000000001?  No but they both look the same on a graph and you may wonder why your filter isn't removing all the zeros.  This might not be the best explaination but here is on of many threads talking about floating point comparision.

 

http://forums.ni.com/t5/LabVIEW/Bug-Two-Doubles-not-detected-as-equal-v8-6/td-p/1066548

0 Kudos
Message 9 of 13
(25,135 Views)

I found this simple "for loop" worked the best for me.

Search the array for the zeroes, then delete them from the array and repeat the number of cells in the array.

Simple, quick, easy.

0 Kudos
Message 10 of 13
(24,053 Views)