4
$\begingroup$

I had a hard time trying to word this question properly; I apologize...

So I have a (very) big list of 0's and 1's:

   AllTimes = {{0}, {0}, {0}, {1}, {1}, ...}

I want to somehow measure the number of continuous 1's (i.e., if the list is {{0}, {1}, {1}, {0}, {1}, {0}} I want to get the values 2 and 1). Is there a way of running a LengthWhile as a loop to give me not just one, but all lengths of continuous 1's?

I've also (miserably) failed to program a different method:

For[i = 1, i <= 100000, i++, t = 0
   If[AllTimes[[{i}]] = 1,
    t = t + 1]
   If[AllTimes[[{i}]] = 0, Print[t], t = 0]]

I wanted each index of AllTimes to be read, and if it's 1, a value of t is added. If the value of an index gives 0, I want the t to be printed and reset back to 0. (I'm totally new to Mathematica programming... so please be gentle...). Although instead of printing, is there a way to also store the value of t before it is reseted to 0?

Thank you for taking your time to read my question...

$\endgroup$
  • $\begingroup$ Welcome to Mathematica SE, I am sure someone will be able to give a good, short solution to the problem you've tried to describe in your post. $\endgroup$ – e.doroskevic Dec 3 '15 at 10:22
  • $\begingroup$ What about: Length /@ SequenceCases[l, {{1} ..}]? $\endgroup$ – Yves Klett Dec 3 '15 at 10:24
  • $\begingroup$ Welcome to Mathematica.SE! 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour and check the faqs! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! $\endgroup$ – user9660 Dec 3 '15 at 10:59
5
$\begingroup$
list = {{0}, {1}, {1}, {0}, {1}, {0}};

Length /@ Cases[Split@Flatten@list, {1 ...}]

{2, 1}

$\endgroup$
  • $\begingroup$ sorry I edited your answer by mistake - I rolled back. $\endgroup$ – Yves Klett Dec 3 '15 at 10:28
5
$\begingroup$

A rather new function (10.1) here:

l = {{0}, {1}, {1}, {0}, {1}, {0}};

Length /@ SequenceCases[l, {{1} ..}]

(* {2, 1} *)

$\endgroup$
3
$\begingroup$

Code:

Length@# & /@ Cases[Split@Flatten[#, 1], {1 ...}] &[{{1}, {1}, {0}, {1}}]

Output:

{2,1}

Reference:
Length
@ | /@ / #
Split
Flatten
Cases

$\endgroup$
  • $\begingroup$ Thanks for the references! It's really nice to understand how each command is used :) $\endgroup$ – Laura Dec 3 '15 at 10:47
  • $\begingroup$ You are very welcome! There is lots of useful information here on Mathematica SE.I would encourage you to visit the most up-voted question section; two posts in particular: "Where can I find examples of good Mathematica programming practices?" && "What are the most common pitfalls awaiting new users?" to get yourself up-to-speed with Mathematica. $\endgroup$ – e.doroskevic Dec 3 '15 at 11:00

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.