5
$\begingroup$

I know this is a novice question but I would like a pattern for a list containing zero or more zeros followed by exactly one 1, followed by zero or more zeros. Something like: {0___,1,0___}

But specifying that 0 must be zero.

$\endgroup$
  • 2
    $\begingroup$ I think what you want is .. and ... that is Repeated and RepeatedNull rather than Blank and its friends. $\endgroup$ – Carlo Aug 8 '14 at 15:19
  • $\begingroup$ Yes, thanks to Carlo and Micahal E2. I am using this to build a row reduction method without loops. $\endgroup$ – John McGee Aug 8 '14 at 16:30
  • $\begingroup$ When using patterns, beware of the difference between 1 and 1. or 0 and 0.. Generally, you can also use /; and specify an arbitrary condition in patterns, e.g.row_ /; Count[row, 1] == 1 && Union[row] == {0, 1} (not necessarily the best solution for this particular problem). $\endgroup$ – Szabolcs Aug 8 '14 at 17:39
5
$\begingroup$

I believe you're looking for RepeatedNull

Count[IdentityMatrix[10], {0 ..., 1, 0 ...}]
(* 10 *)
| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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