Why is histcount ignoring one of the edge values in my matrix?

14 vues (au cours des 30 derniers jours)
Andri Graf
Andri Graf le 14 Mai 2021
Commenté : Andri Graf le 14 Mai 2021
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
Andri Graf
Andri Graf le 14 Mai 2021
Can I also attach the data files as a txt file? this is the original data format I get from my experiments. I printed both (edge values and count as a txt file and there i saw which one is "ignored") also i tried to create a matrix with edge values and counts in a column next to each other but it doesn't work since its not the same length, that's where i originally found the error.
Jan
Jan le 14 Mai 2021
Please do not explain, what you expect your code to do, but post the code, which reproduces the problem.

Connectez-vous pour commenter.

Réponse acceptée

David Hill
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
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)
  1 commentaire
Andri Graf
Andri Graf le 14 Mai 2021
thanks for the explanation! I used the code from David Hill and it seems like you are right and the old histc works better in this case.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Statistics and Machine Learning Toolbox dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by