Sort array elements by the frequency of its elements

廉价感情. 提交于 2020-01-04 01:28:07

问题


Is it possible in matlab/octave to use the sort function to sort an array based on the relative frequency of their elements?

For example the array

m= [4,4,4,10,10,10,4,4,5]

should result in this array:

[5,10,10,10,4,4,4,4,4]

5 is the less frequent element and is on the top while 4 is the most frequent and it's on bottom. Should one use the indices provided by histcount?


回答1:


The following code first calculates how often each element occurs and then uses runLengthDecode to expand the unique elements.

m = [4,4,4,10,10,10,4,4,5];

u_m = unique(m);

elem_count = histc(m,u_m);
[elem_count, idx] = sort(elem_count);

m_sorted = runLengthDecode(elem_count, u_m(idx));

The definition of runLengthDecode is copied from this answer:

For MATLAB R2015a+:

function V = runLengthDecode(runLengths, values)
if nargin<2
    values = 1:numel(runLengths);
end
V = repelem(values, runLengths);
end

For versions before R2015a:

function V = runLengthDecode(runLengths, values)
%// Actual computation using column vectors
V = cumsum(accumarray(cumsum([1; runLengths(:)]), 1));
V = V(1:end-1);
%// In case of second argument
if nargin>1
    V = reshape(values(V),[],1);
end
%// If original was a row vector, transpose
if size(runLengths,2)>1
    V = V.'; %'
end
end



回答2:


One way would be to use accumarray to find the count of each number (I suspect you can use histcounts(m,max(m))) but then you have to clear all the 0s).

m = [4,4,4,10,10,10,4,4,5];

[~,~,subs]=unique(m);
freq = accumarray(subs,subs,[],@numel);
[~,i2] = sort(freq(subs),'descend');

m(i2)

By combinging my approach with that of m.s. you can get a simpler solution:

m = [4,4,4,10,10,10,4,4,5];

[U,~,i1]=unique(m);
freq= histc(m,U);
[~,i2] = sort(freq(i1),'descend');

m(i2)



回答3:


You could count the number of repetitions with bsxfun, sort that, and apply that sorting to m:

[~, ind] = sort(sum(bsxfun(@eq,m,m.')));
result = m(ind);


来源:https://stackoverflow.com/questions/31671518/sort-array-elements-by-the-frequency-of-its-elements

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!