05-23-2012 09:23 AM - edited 05-23-2012 09:23 AM
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
05-23-2012 09:34 AM - edited 05-23-2012 09:37 AM
Its very simple try the following method ( there are lot of ways to do it).
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)
05-23-2012 10:11 AM - edited 05-23-2012 10:14 AM
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?
05-23-2012 11:20 AM
Why does this sound like a homework problem?
05-23-2012 11:51 AM
@smercurio_fc wrote:
Why does this sound like a homework problem?
It is in the difference between "I have to" and "I want to". 😄
05-23-2012 11:59 AM
@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.
05-23-2012 12:05 PM
@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". 😄
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.
05-23-2012 12:09 PM
@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". 😄
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.
05-23-2012 12:20 PM - edited 05-23-2012 12:22 PM
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?
05-23-2012 12:31 PM
I hated people like you guys when I was in school! haha