9
$\begingroup$

I need to determine how many strictly increasing sequences of a certain length (e.g. 5) are contained in a list of integers. I would be very happy if someone could help me. A sequence would be considered strictly increasing if a<b<c.

list=BlockRandom[RandomInteger[11,{11111}],RandomSeeding->1234];
$\endgroup$

2 Answers 2

10
$\begingroup$
length = 5;

Count[And @@@ Positive[Partition[Differences[list], length-1, 1]], True]
42

You can also use:

Count[Positive[Partition[Differences[list], length-1, 1]], {True ..}]

Count[Partition[Unitize @ Ramp @ Differences[list], length-1, 1], {1 ..}]

Count[Subsequences[Unitize @ Ramp @ Differences[list], {length-1}], {1 ..}]

Total[Times @@@ Subsequences[Unitize @ Ramp @ Differences @ list, {length-1}]]

Total @ BlockMap[Apply[Times], Unitize @ Ramp @ Differences @ list, length-1, 1]

Count[Less @@@ Partition[list, length, 1], True]

Count[Subsequences[list, {length}], _?(Apply[Less])]

Count[BlockMap[Apply[Less], list, length, 1], True]

Total @ Boole[Less @@@ Subsequences[list, {length}]]

Total @ Boole[BlockMap[Apply[Less], list, length, 1]]

Length @ Select[Partition[list, length, 1], Apply[Less]]

Total[Boole[Less @@@ Partition[list, length, 1]]]
$\endgroup$
1
$\begingroup$
length = 5;

Length @ SequenceCases[list, {p : Repeated[_, {length}]} /; 
    Apply[Less] @ {p}, Overlaps -> True]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.