MATLAB: Selecting values that are repeating most of the times in an array

array operationsMATLABrepeating values

[updated]Hi I have an array A = [8 8 8 10 10 13 13 13 5 17 3 89] I want to select 4 (can be any number) values out of it but the values that are repeating most of the time should get priority. So i want a new matrix that should look like B = [8 13 10 5], the rest of the values can be anything i don't care. and i don't care about the order as well. Thank you.

Best Answer

[x,~,c] = unique(A);
[~,ii] = sort(accumarray(c,1),'descend');
B = x(ii(1:4));