I have a table of 200 elements, elements are '0', '1', '2'. I have participate them into groups for 20 elements each one. Then I have calculate how many '0', '1', '2' are in each part. And then in vectors n1, n2, n0 write the answers. For example: n1=(3,4,5,2,3,4,5,6,6,1); so there are 3 elements of '1' in first part and so on. How can I do that?
Asked
Active
Viewed 148 times
0
1 Answers
0
I guess this will do what you want! Here an example of the kind of groups of 20 you mentioned. I assume '0', '1', '2' in your elements list are strings.
list = RandomChoice[{"0", "1", "2"}, {10, 20}];
Now the counting is easy. and you get those n0,n1,n2.
Transpose@(Sort[Tally[#]] & /@ ToExpression[list])[[All, All, 2]]
{{2, 9, 8, 7, 7, 9, 4, 10, 7, 8}, {11, 5, 3, 7, 5, 4, 6, 2, 3, 5}, {7, 6, 9, 6, 8, 7, 10, 8, 10, 7}}
As mentioned in the comment here goes the BinCount version.
Transpose[BinCounts[#, {0, 10, 1}] & /@ ToExpression[list]][[1 ;; 3]]
PlatoManiac
- 14,723
- 2
- 42
- 74
-
1
BinCountsmight be better (this will break if any count happens to be zero) – george2079 Apr 14 '14 at 14:22
PartitionandTally– george2079 Apr 14 '14 at 10:52Partitionwhich you were shown in answer to your prior question. If this is not what you want you can still edit this question to clarify. – Mr.Wizard Apr 14 '14 at 20:04