histogram, indices of values used from the vector for each class?
Show older comments
Hi,
could you help me to find out standard deviation calculated from the of 2nd column values coresponding to the histogram classes of the 1st column in a given 2 columns matrix?
for example in data
c1 = [ 1 3 2 1 3 1 2 2 3 3 2 2 ]'; c2 = [ 0.5 1.1 0.65 0.55 0.9 0.6 0.7 0.75 0.95 1 0.8 0.85]';
so for the group 1 standard deviation would be 0.05
histogram of c1 is easy, hist(c1, 1:1:3); or [n, xout] = hist(c1, 1:1:3); but how to get indices of histogram c1 groups (c1_i1) which could be used to calculate std of c2 values? c2_std1 = std(c2(c1_i1))? is it currently possible by using histogram?
one solution is by using find (not histogram), e.g. c1_i1 = find (0 <c1 & c1 < 2); c2_std1 = std(c2(c1_i1))
but this is quite complicated for large matrices whith many classes of 1st column.
Thanks, Boris
Accepted Answer
More Answers (2)
Sean de Wolski
on 23 Nov 2011
accumarray(c1,c2,[],@std)
I have it running about 3x faster than grpstats too:
c1 = [ 1 3 2 1 3 1 2 2 3 3 2 2 ]';
c2 = [ 0.5 1.1 0.65 0.55 0.9 0.6 0.7 0.75 0.95 1 0.8 0.85]';
c1 = repmat(c1,100,1);
c2 = repmat(c2,100,1);
t1 = 0;
t2 = 0;
for ii = 1:100
tic
grpstats(c2,c1,'std');
t1 = t1+toc;
tic
accumarray(c1,c2,[],@std);
t2 = t2+toc;
end
Boris
on 23 Nov 2011
Categories
Find more on Histograms in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!