how to find the second most repeated value in vector

60 vues (au cours des 30 derniers jours)
mary m
mary m le 25 Août 2017
Commenté : Ambati Sathvik le 2 Juil 2020
how to find the second most repeated value in vector x=[1 2 2 3 5 5 5] i use (mode ) to find the most repeated and frequency [m,f]=mode(x) m=5 the number repeated f=3 the freq.
now,i want to find the second repeated, can help me please

Réponse acceptée

Stephen23
Stephen23 le 25 Août 2017
Modifié(e) : Stephen23 le 25 Août 2017
>> [n,bin] = hist(x,unique(x));
>> [~,idx] = sort(-n);
>> n(idx) % count instances
ans =
3 2 1 1
>> bin(idx) % corresponding values
ans =
5 2 1 3
>>

Plus de réponses (4)

Walter Roberson
Walter Roberson le 25 Août 2017
Delete all the copies of m out of x and take the mode again.
  1 commentaire
Apoorva Srivastava
Apoorva Srivastava le 16 Juin 2019
Modifié(e) : Apoorva Srivastava le 16 Juin 2019
% For a vector x:
mode(x(find(x ~= mode(x))))
% In general, if x is a matrix (valid from R2018b onwards)
mode(x(find(x ~= mode(x, 'all'))), 'all')

Connectez-vous pour commenter.


Andrei Bobrov
Andrei Bobrov le 25 Août 2017
[g,v] = findgroups(x);
ii = accumarray(g(:),1);
jj = find(ii > 1);
out = [v(jj(2)), ii(jj(2))]

alexander Mcghee
alexander Mcghee le 17 Sep 2019
X = [1 1 1 1 1 5 5 5] ;
m = mode(X) % -> m=1
X(X==m) = NaN ;
m = mode(X) % -> m=5

mary m
mary m le 26 Août 2017
thank you for all.. ^_^

Catégories

En savoir plus sur Financial 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!

Translated by