Why is histcount ignoring one of the edge values in my matrix?
조회 수: 14 (최근 30일)
이전 댓글 표시
I use this code to find all unique values of the second column of a matrix and then count how many times each unique value is present in the same column. Surprisingly it doesn't count one of the unique values even though it is present 10 times in this row, every other value is counted correctly. Why is this the case and can i prevent this in any way?
I added the original data as a txt file. the second column is the one i'm extracting the values from. the ultimate goal is to extract all unique values and how many times they show up and then put them both next to each other in a new matrix with the values in the first column and the counts in the second column. At the moment this does not work since i get 23 unique values but only 22 counts.
Code:
Aedges = unique(pkdata3(:,2));
Acounts = histcounts(pkdata3(:,2), Aedges);
These are the unique values :
1567.8
1569.5
1571.3
1573.1
1574.8
1576.6
1578.4
1580.1
1581.9
1583.7
1585.4
1587.2
1588.9
1590.7
1592.5
1594.2
1596
1597.7
1599.5
1601.3
1603
1604.8 (this one is beeing ignored for some reason)
1606.5
댓글 수: 3
Jan
2021년 5월 14일
Please do not explain, what you expect your code to do, but post the code, which reproduces the problem.
채택된 답변
David Hill
2021년 5월 14일
편집: David Hill
2021년 5월 14일
a=readmatrix('GPeak_Histogram_Data.txt');
Aedges = unique(a(:,2));
Acounts = histc(a(:,2), Aedges);%use histc
or
a=readmatrix('GPeak_Histogram_Data.txt');
Aedges = unique(a(:,2));
Aedges = [Aedges;Aedges(end)+1];%add an edge at the end
Acounts = histcounts(a(:,2), Aedges);
추가 답변 (1개)
Jan
2021년 5월 14일
I still have the impression, thet histcounts works less intuitive than the older hist. Remember, that the rightmost edge is handled differently, see: doc histcounts:
[N,edges] = histcounts(X,edges) sorts X into bins with the bin edges specified by the vector, edges. The value X(i) is in the kth bin if edges(k) ≤ X(i) < edges(k+1). The last bin also includes the right bin edge, so that it contains X(i) if edges(end-1) ≤ X(i) ≤ edges(end).
Maybe this is more efficient for your problem:
[Aedges, ~, index] = unique(pkdata3(:,2));
n = histcounts(index, 'BinMethod', 'integers');
% Or:
n = accumarray(index, 1)
참고 항목
카테고리
Help Center 및 File Exchange에서 Statistics and Machine Learning Toolbox에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!