"logo.gif"  
Main
Read Online
Download PDF
Additional resources
Revision history
Known typos/bugs
Report a bug
License terms
About the author
"node368_1.gif" "node368_2.gif" "node368_3.gif"

4.3.2.2   Example: non-zero integer subsequences

Consider a following problem : we are given a list of integers, some of which can be zero. The task is to extract from the list all subsequences of consecutive non - zero elements.

For example, this will be our test data set :

"node368_4.gif"

"node368_5.gif"

The first step in solving this problem will be to use Split (see section 3.10.3) to split the elements into sublists whenever zero is encountered :

"node368_6.gif"

"node368_7.gif"

We have however captured some zeros into our sublists, so we have to delete them (note the level specification) :

"node368_8.gif"

"node368_9.gif"

Finally, the previous operation in general produces some number of empty lists from sublists containing a single zero (there can not be sublists containing several zeros - why?). We have now to delete them as well:

"node368_10.gif"

"node368_11.gif"

We now package all the steps into a function :

"node368_12.gif"

Note the use of a named pattern and BlankSequence, to better  restrict the argument. Check :

"node368_13.gif"

"node368_14.gif"

"node368_15.gif" "node368_16.gif" "node368_17.gif"

Created by Wolfram Mathematica 6.0  (05 February 2009) Valid XHTML 1.1!