Trying to find a histogram half-sum point

I'm trying to find the x coordinate in a histogram that describes the point where the sum of the values is exactly half (basically like the half-life in an exponential decay graph).
My code that doesn't quite work: (avoiding the use of loops)
h = hist(dataset);
% ex: h = [3 45 1 4 6 3 4 6 8 23 6 3 44];
n = 1:1:length(h);
a(n) = (sum(h(1:n)) <= sum(h)/2); % Returns a Boolean matrix which identifies where the half-sum point is
x = find(a); % Returns the non-zero indices of a, which can be used as an accessing array
fprintf('Angular Resolution Metric is at %f', x)
The problem is that (sum(h(1:n)) < sum(h)/2) isn't working... any tips?
(Also, I realize that it should give me something like [1 1 1 1 1 0 0 0 0 0 0 0] ...etc. so I'd just reference the maximum x value to get the point)
edit:
The following code works, but I'd like to avoid using loops, so if anyone figures out how to write this w/out loops that'd be good to know:
n=1;
while n < length(h)
if sum(h(1:n)) < sum(h)/2
n=n+1;
else
x=n;
fprintf('Angular Resolution Metric is at %f \n', x)
break
end
end

 Réponse acceptée

Paulo Silva
Paulo Silva le 25 Juil 2011
h = [3 45 1 4 6 3 4 6 8 23 6 3 44]
x=sum(cumsum(h) < sum(h)/2)
fprintf('Angular Resolution Metric is at %d \n', x)

Plus de réponses (1)

Bjorn Gustavsson
Bjorn Gustavsson le 25 Juil 2011
This slight modification seems to work as I expect it would:
sum_h = sum(h)
cumsum(a)
a = cumsum(h) <= sum_h/2
This produces:
sum_h =
156
cumsum_h = 3 48 49 53 59 62 66 72 80 103 109 112 156
a = 1 1 1 1 1 1 1 1 0 0 0 0 0
HTH

Catégories

En savoir plus sur Data Distribution Plots dans Centre d'aide et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by