필터 지우기
필터 지우기

histc and bin-width determination

조회 수: 2 (최근 30일)
MiauMiau
MiauMiau 2015년 5월 7일
댓글: Image Analyst 2015년 5월 7일
Hi
Given I have the following data:
A = [100 150 190 200 250 300 350 370 390 400 400]
And I want 3 bins: From 100 to 200, 200 to 300, and 300 to 400.
How do I do that with histc?
What I tried so far was:
edges = linspace(min(stim_durations),max(stim_durations),4);
counts = histc(stim_durations, edges);
But that results in 4 bins with "400" having its own bin..how can I solve that?

채택된 답변

Guillaume
Guillaume 2015년 5월 7일
편집: Guillaume 2015년 5월 7일
The simplest solution on a recent enough version of matlab (2014b or newer) is to use histcounts which behaves exactly as you want:
>>A = [100 150 190 200 250 300 350 370 390 400 400];
>>hiscounts(A, [100 200 300 400])
ans =
3 2 6
As per histc documentation, the last edge is its own bin and you can't do anything about that. If you want 400 to be included in the third bin and still use histc, you have to shift the edge of the 4th value slightly above 400 (and discard that last bin):
A = [100 150 190 200 250 300 350 370 390 400 400];
edges = linspace(min(A),max(A),4);
edges(end) = edges(end) + eps(edges(end)); %or any value greater than eps(400);
counts = histc(A, edges);
counts = counts(1:end-1)
  댓글 수: 3
MiauMiau
MiauMiau 2015년 5월 7일
perfect Guillaume hat was exactly what I was looking for...too bad histc is not available in older versions..
Image Analyst
Image Analyst 2015년 5월 7일
histc IS available in older versions. It's now been deprecated in favor of histcounts, meaning that in new versions they recommend you use histcounts and not histc. I didn't mention it in my answer because it seems like hardly anyone except me has the latest version.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Walter Roberson
Walter Roberson 2015년 5월 7일
edges = linspace(min(stim_durations),max(stim_durations),4);
edges(end) = [];
That is, construct 4 bins and discard the last of them. The final bin will be everything from the previous value upwards.
Please pay attention to whether you want your bins to be 100 <= x <= 200, 200 <= x <= 300, or if you want them to be 100 <= x < 200, 200 <= x < 300 or 100 < x <= 200, 200 < x <= 300 . Those are slightly different boundary conditions that can be pretty meaningful.
  댓글 수: 1
MiauMiau
MiauMiau 2015년 5월 7일
편집: MiauMiau 2015년 5월 7일
No I mean I want them up to 400 If I delete the last element of edges it will be only up to 300 But what I do not want is that the edge 400 itself has an own bin...
So one bin from 100 to 200 one from 200 to 300 and one from 300 to 400 (if the edges are included or not is not relevant at that point)

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Data Distribution Plots에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by