Reputation: 830
I have a simple problem.
I have this code:
test = [30,40,60,30,20,10,5,5,3]
bar(test)
The bar represent the child number by family. For example, 30 family have 0 child and 10 family have 5 children.
I want to obtain the child average by family and of course I can't just use mean(test).
Upvotes: 0
Views: 168
Reputation: 13923
We can easily convert your data in a form where each entry of a vector represents an observation of a family with a value for the number of children. This way we generate a vector with length of the total number of families. Each entry is a number representing the amount of children of the observed family.
To do this we can use arrayfun
and repmat
to repeat the entries in cnt
as many times as there are corresponding families in test
. Since the output of arrayfun
is a cell-array, we need to use cell2mat
to convert it back to a 'normal' matrix. After that, obs
is the above mentioned vector. Now we can simply use mean
or var
to calculate what you need.
Here is an example:
test = [30,40,60,30,20,10, 5 ,5, 3] % families
cnt = [ 0, 1, 2, 3, 4, 5, 6, 7, 8] % children per family in test
obs = arrayfun(@(s,c)repmat(c,1,s),test,cnt,'UniformOutput',false);
obs = cell2mat(obs);
mean(obs) % mean
var(obs) % variance
This is the result:
ans =
2.3103
ans =
3.2349
Note: I assume that there are 30 families with 0 children (as your text says) and not 30 families with one child (as your bar-plot says). Just adjust cnt
to match your needs.
To get test
and cnt
back from obs
, you can use hist
like this:
[test,cnt] = hist(obs,unique(obs));
For the creation of your bar-plot, use bar
with two arguments. This way you have the correct x-values (in this case cnt
).
bar(cnt,test)
xlabel('Children per family')
ylabel('Number of Families')
Upvotes: 2