NI Home
Cart Cart | Help
Hello Events Academic NI Developer Zone Support Solutions Products & Services Contact NI MyNI
You are here: 
NI Home > NI Developer Zone > NI Discussion Forums


Reply
Member
tintin99
Posts: 40
0 Kudos

How to remove 0s from an array

[ Edited ]

Hi , I have got an array and I need to remove 0s from this array and then just keep the min and max values for the indexes  in between. Attached is a pic which can show the problem better. I would really appreciate if you could help me with an example how to do this task

Active Participant
P@Anand
Posts: 1,617
0 Kudos

Re: How to remove 0s from an array

[ Edited ]

Its very simple try the following method ( there are lot of ways to do it).

 

Array without zero.png

 

Array without zero 1.png

 

Edit: Hold on you need to keep the min and max values only?. How you will define that? If I have 12, 15, 13, 17 what are the values I need to have?

 

Good luck (I love posting code snippets)

-----

I don't believe in Hard work. I love my work :smileyhappy:
Knight of NI
altenbach
Posts: 22,874
0 Kudos

Re: How to remove 0s from an array

[ Edited ]

If the input array is large, I would strongly recommend a solution that operates in place. The speed can be orders of magnitude faster.

 

You can do something similar as described in this discussion.

 

Your image is actually not very clear, because it shows two output arrays. Can you be more specific what you actually want? Do you want to keep two elements (min&max) for each nonzero stretch of data? What if there is only one nonzero value? Should the output be a 2xN 2D array?

 


LabVIEW Champion . Oh, by the way, I work for peanutsKudos .

Knight of NI
Posts: 18,064
0 Kudos

Re: How to remove 0s from an array

Why does this sound like a homework problem?

Knight of NI
altenbach
Posts: 22,874

Re: How to remove 0s from an array


smercurio_fc wrote:

Why does this sound like a homework problem?


It is in the difference between "I have to" and "I want to". :smileyvery-happy:


LabVIEW Champion . Oh, by the way, I work for peanutsKudos .

Trusted Enthusiast
for(imstuck)
Posts: 2,056
0 Kudos

Re: How to remove 0s from an array


altenbach wrote:

If the input array is large, I would strongly recommend a solution that operates in place. The speed can be orders of magnitude faster.

 


Meh, even if it's small, it's good practice to do it in place anyways.



Greg Freeman - CLA, LV Version 2012, 2011, and 2010

G Systems, LP


Trusted Enthusiast
crossrulz
Posts: 3,769
0 Kudos

Re: How to remove 0s from an array


altenbach wrote:

smercurio_fc wrote:

Why does this sound like a homework problem?


It is in the difference between "I have to" and "I want to". :smileyvery-happy:


Plus it's end of May and schools are finishing up.

 

Anyways, I think he's trying to use the 0 as a delimiter and then find the max and min for each section.



Kudos always welcome for helpful posts...Kudos always welcome for any post.
Knight of NI
Posts: 18,064
0 Kudos

Re: How to remove 0s from an array


crossrulz wrote:

altenbach wrote:

smercurio_fc wrote:

Why does this sound like a homework problem?


It is in the difference between "I have to" and "I want to". :smileyvery-happy:


Plus it's end of May and schools are finishing up.


Which is why we've been inundated with "final project" questions and pleas for "write this code for me" requests for suggestions on how to write the code.

Trusted Enthusiast
crossrulz
Posts: 3,769

Re: How to remove 0s from an array

[ Edited ]

Based on my understanding at I stated above, here's my solution.  It was a fun 10 minute exercise.  I am purposefully uploading a buggy solution just in case it is a homework assignment.

 

Will it be easier to debug my code or rewrite from scratch?:smileytongue:



Kudos always welcome for helpful posts...Kudos always welcome for any post.
Trusted Enthusiast
for(imstuck)
Posts: 2,056
0 Kudos

Re: How to remove 0s from an array

I hated people like you guys when I was in school! haha :smileyfrustrated:



Greg Freeman - CLA, LV Version 2012, 2011, and 2010

G Systems, LP


By using this web site, you accept the Terms of Use for this web site. Please read these Terms of Use carefully before using any part of this site. Please go here for information on ni.com's copyright infringement policy.
My Profile | Privacy | Legal | Contact NI © 2011 National Instruments Corporation. All rights reserved.    |    E-Mail this Page E-Mail this Page