Why is histcount ignoring one of the edge values in my matrix?
14 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Jan
le 14 Mai 2021
Please do not explain, what you expect your code to do, but post the code, which reproduces the problem.
Réponse acceptée
David Hill
le 14 Mai 2021
Modifié(e) : David Hill
le 14 Mai 2021
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);
Plus de réponses (1)
Jan
le 14 Mai 2021
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)
Voir également
Catégories
En savoir plus sur Statistics and Machine Learning Toolbox dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!