Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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.

share|improve this question
    
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. –  Kuba Apr 15 at 8:42
    
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. –  antalstrempel Apr 15 at 10:10
    
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? –  Kuba Apr 15 at 10:14

1 Answer 1

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}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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