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"   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 :



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 :



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



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:



We now package all the steps into a function :


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



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

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