Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Repeated Pattern

Can someone suggest how a pattern would be constructed to extract the first list of contiguous numbers from this data?

sample = {52.2624, 54.4003, 60.7418, 61.3801, 62.6397, 61.7992,
   63.2282, "", "", "", "", "", "", "", "", "", "", 62.3921, 61.897,
   60.299, 59.053, 61.3778, 64.3724, 63.4251, 78.1912, 79.7451,
   80.4741, "", 81.324, 79.9114, 93.7509};

I have tried variations like sample //. {useable : _?NumberQ .., ___} -> {useable} to no avail.

useable = TakeWhile[sample, NumberQ] works well, but I would like to know how to do it using pattern matching.

like image 589
Chris Degnen Avatar asked Dec 08 '11 12:12

Chris Degnen


People also ask

What is an example of a repeated pattern?

Most repeating patterns in the environment occur in manufactured objects. Some examples are tiles, pavers, windows, zebra crossings and railway lines. Such objects are generally assembled from units that are very nearly identical.

What is a repeated pattern called in art?

A pattern is a design in which lines, shapes, forms or colours are repeated. The part that is repeated is called a motif. Patterns can be regular or irregular. Art and Design.


2 Answers

Trying to preserve your logic:

 sample /. {useable : Longest[_?NumberQ ..], ___} -> {useable}

If you want the longest numeric sequence:

sample /. {___, useable : Longest[_?NumberQ ..], ___} -> {useable}

Edit

To get all the numeric sequences:

Cases[SplitBy[sample, NumberQ], {_?NumberQ ..}]

or

Last@Reap[sample //. {x___, useable : Longest[_?NumberQ ..], y___} :> 
                                              (Sow@{useable}; {x}~Join~{y})]
like image 150
Dr. belisarius Avatar answered Nov 08 '22 22:11

Dr. belisarius


One way would be

sample /. {Longest[useable___?NumberQ], ___} :> {useable}

which returns {52.2624, 54.4003, 60.7418, 61.3801, 62.6397, 61.7992, 63.2282} from your sample.

like image 30
Simon Avatar answered Nov 08 '22 20:11

Simon