1
$\begingroup$

I made a table giving me 1's and 2's

In[23]:= S = Join[Table[1, 1], Table[2, 1]]

Out[23]= {1, 2}

And then I made it bigger:

T = RandomChoice[S, 15*365];

And then I separated it:

R = Partition[T, 15];

It looks something like this:

{{2, 1, 2, 2, 2, 1, 1, 2, 1, 1, 1, 1, 1, 2, 1}, {2, 2, 2, 2, 1, 1, 2, 
  1, 2, 2, 1, 2, 1, 1, 2}, {2, 1, 1, 2, 1, 2, 2, 2, 2, 2, 1, 1, 2, 2, 
  2}, {2, 2, 2, 2, 2, 1, 1, 1, 2, 1, 1, 2, 2, 1, 1}, {1, 1, 1, 1, 1, 
  2, 2, 2, 1, 2, 2, 2, 2, 1, 1}, {2, 2, 2, 2, 2, 2, 1, 1, 2, 1, 2, 1, 
  1, 1, 1}, {1, 1, 2, 1, 2, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1}, {1, 1, 1, 
  2, 1, 2, 2, 1, 1, 2, 2, 1, 2, 2, 2}, {2, 1, 2, 2, 1, 2, 2, 1, 2, 2, 
  1, 2, 2, 2, 2}, {2, 2, 2, 2, 1, 2, 1, 1, 1, 1, 2, 2, 2, 1, 2}, {1, 
  2, 1, 2, 2, 1, 2, 1, 2, 2, 1, 2, 1, 2, 1}, {1, 1, 2, 1, 2, 1, 2, 2, 
  1, 2, 1, 1, 2, 1, 2}, {2, 1, 2, 1, 2, 1, 2, 2, 1, 1, 2, 1, 2, 2, 
  1}, {2, 1, 2, 2, 1, 2, 2, 2, 2, 1, 1, 1, 2, 1, 1}, {2, 2, 1, 1, 1, 
  1, 2, 2, 1, 2, 1, 1, 2, 1, 1}, {1, 2, 1, 2, 2, 2, 1, 2, 2, 2, 2, 1, 
  1, 2, 2},

but much bigger. I want to count the number of sublists with more than 9 2's only. Is there anyway to do this? Is there a way to treat the sublists as a separate unit, as opposed to the entire list of R? Thanks.

$\endgroup$
  • 4
    $\begingroup$ Don't use uppercase initials for your symbols: you will eventually end up clashing with some built-in. Tr[UnitStep[Total[R - 1, {2}] - 10]] will quickly do what you want. $\endgroup$ – ciao Nov 7 at 21:36
  • $\begingroup$ Jus use this for R= RandomChoice[{1,2}, {365,15}]; $\endgroup$ – OkkesDulgerci Nov 8 at 5:50
1
$\begingroup$

This might work for you ... let your list be called r, then try

Count[Map[Count[#, 2] > 9 &, r], True]

If you need further explanation, ask for it.

$\endgroup$
  • $\begingroup$ Yes it worked, thank you! $\endgroup$ – Neo Alcetrutz Nov 7 at 21:51
  • 1
    $\begingroup$ Slightly simpler: Count[r, x_ /; Count[x, 2] > 9] $\endgroup$ – Jason B. Nov 7 at 21:55
  • 1
    $\begingroup$ Also Length[Select[r, Count[#, 2] > 9 &]] $\endgroup$ – MelaGo Nov 8 at 1:37
0
$\begingroup$

Just another way for this specific context:

r = Partition[RandomChoice[{1, 2}, 15 365], 15];
Count[Total /@ (r - 1), _?(# > 9 &)]

However, as comments illustrate there are many ways to do this.

$\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.