Trying to find a histogram half-sum point
조회 수: 2 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
채택된 답변
Paulo Silva
2011년 7월 25일
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)
추가 답변 (1개)
Bjorn Gustavsson
2011년 7월 25일
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
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Distribution Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!