histcounts error in place of histc

3 vues (au cours des 30 derniers jours)
Pramit Biswas
Pramit Biswas le 19 Jan 2018
Modifié(e) : Jan le 2 Fév 2018
Objective: finding the frequency of unique elements in an array.
A = [0]
F = histc(A,unique(A))
% F = histcounts(A,unique(A))
Expected output is 1 for A=[0] or 2 for A=[0 0], which is perfectly done by histc. As currently histc is not recommended. Use histcounts instead. I tried, but encountered error. Notes for change

Réponses (3)

ANKUR KUMAR
ANKUR KUMAR le 1 Fév 2018
Try this.
A=randi(5,1,15)
a=histcounts(A)
bar(unique(A),a)
If you are facing problem using histcounts, then you can find the frequency of unique elements without using histcounts
clc
clear
A=randi(5,1,15)
B=unique(A)
for ii=1:length(B)
id(ii)=length(find(A==B(ii)))
end
bar(B,id)
  1 commentaire
Jan
Jan le 2 Fév 2018
Faster:
id = zeros(1, length(B)); % Pre-allocate
for ii = 1:length(B)
id(ii) = sum(A==B(ii)); % Without FIND
end

Connectez-vous pour commenter.


Sean de Wolski
Sean de Wolski le 1 Fév 2018
The edges needs to be at least two elements. I usually do this to make one side (either negative or positive depending on context) open ended:
A = 0
F = histc(A,unique(A))
F2 = histcounts(A,[unique(A), inf])
  1 commentaire
Jan
Jan le 2 Fév 2018
Modifié(e) : Jan le 2 Fév 2018
+1: This is the best translation of the arguments for the old histc to the new histcounts.
What a pity: For large arrays, creating the temporary vector [unique(A), inf] wastes time compared to the histc method. After all these years of using this function successfully, I'm running my own C-Mex function now instead of histcounts. A bad decision of TMW.

Connectez-vous pour commenter.


Jan
Jan le 2 Fév 2018
Modifié(e) : Jan le 2 Fév 2018
What a pity that the handy histc is deprecated now.
A = randi(5,1,15);
[uniqA, ~, iA] = unique(A);
N = histcounts(iA, 'BinMethod', 'integers');
Now the element uniqA(k) appears N(k) times.
This is fast, but limited to 65536 elements.

Catégories

En savoir plus sur Resizing and Reshaping Matrices dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by