MATLAB: Trying to find a histogram half-sum point

booleanhalf-lifehistogramno loops

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

Best Answer

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)