3
$\begingroup$

I have a one-dimensional array of 200 elements consisting of the numbers 0, 1, 2. This array needs to be divided into 10 groups (20 items each). Then, for each group, I want to count how many of the elements 0, 1, 2 is in each of them. These findings should be stored in three arrays, n0, n1, n2: n0 containing the statistics for the zeros, n1 the statistics for the ones, n2 the statistics for the threes.

For example, one might get n0 = {3, 4, 3, 6, 8, 12, 1, 9, 15, 16, ...}, so in the first 20 elements of the original array there were 3 zeros, in the following 20 there were 4 zeros, and so on.

I have Mathematica 5.0. Please keep in mind that functions first appearing in later versions are not available to me.

$\endgroup$
3
  • $\begingroup$ Unfortunately Tally is new in V6 but Partition/Count/Sow/Reap are available to you. Could you please provide a minimal example, e.g. 20 elements divided on 4 groups and a desired output. Now it is unclear what do you want to after counting them. $\endgroup$ Commented Apr 15, 2014 at 8:42
  • $\begingroup$ I have a table {2, 0, 0, 0, 0, 2, 0, 0, 1, 0, 1, 0, 0, 1, 2, 1, 0, 0, 0, 0}.Divide in 4 groups (this groups I don't show during the program) {2, 0, 0, 0, 0}, {2, 0, 0, 1, 0}, {1, 0, 0, 1, 2}, {1, 0, 0, 0, 0} In groups I count elements 0,1,2 and write them into vectors: n0={4,3,2,4}(number of 0 in each group), n1={0,1,2,1}(number of 1 in each group), n2={1,1,1,0}(number of 2 in each group).This vectors (n0,n1,n2) I must see in the program after. Next step: this vectors I have put in one matrix where the row are this vectors. $\endgroup$ Commented Apr 15, 2014 at 10:10
  • $\begingroup$ Your previous question was closed as a duplicate and indeed, methods there need Tally which you can't use. But the first answer there does not use it so you can adapt it, can't you? $\endgroup$ Commented Apr 15, 2014 at 10:14

2 Answers 2

5
$\begingroup$

All you need are Partition and Count. Both were available in V1.1. Lets take it step by step.

First some random data. For demonstration purposes 20 elements will be enough.

SeedRandom[42]; data = RandomInteger[{0, 2}, 20] 
{1, 2, 1, 0, 0, 2, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 2, 2, 1, 2} 

Again for demonstration purposes, 5 groups of 4 elements each will suffice.

groups = Partition[data, 4] 
{{1, 2, 1, 0}, {0, 2, 0, 0}, {2, 0, 0, 0}, {0, 0, 0, 0}, {2, 2, 1, 2}} 

Now map a function that counts the zeros in each group across the list of groups.

 n0 = Count[#, 0] & /@ groups 
{1, 3, 3, 4, 0} 

Similarly for the ones and twos.

n1 = Count[#, 1] & /@ groups n2 = Count[#, 2] & /@ groups 
{2, 0, 0, 0, 1} {1, 1, 1, 0, 3} 
$\endgroup$
2
$\begingroup$

I have Mathematica 5.0....

So the following may be of interest for users with versions 10+:

ClearAll[countsByGroups] countsByGroups = Transpose[{0, 1, 2} /. Normal[KeySort /@ Counts /@ Partition[##]]] &; 

Examples:

SeedRandom[42]; data = RandomInteger[{0, 2}, 20] 

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

Partition[data, 4] 

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

countsByGroups[data, 4] 

{{1, 3, 3, 4, 0}, {2, 1, 1, 1, 1}, {1, 1, 1, 2, 3}}

Partition[data, 5] 

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

countsByGroups[data, 5] 

{{2, 3, 5, 1}, {2, 1, 1, 1}, {1, 2, 2, 3}}

$\endgroup$
2
  • $\begingroup$ ...with the caveat that Counts[] and KeySort[] (and associations for that matter) were not around in version 5. $\endgroup$ Commented Oct 12, 2017 at 14:07
  • $\begingroup$ @J.M., just noticed v5:) $\endgroup$ Commented Oct 12, 2017 at 14:30

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.