Matlab - Most duplicate values ​​in an array (not just in mode)

I have an array with numbers that range from 1 to 4. I need to know which are / are values ​​that repeat more. If there is a draw, I need to know what values, so I can perform some operations.

Example:

a = [1 1 1 2 2 2 3 4]
Output = [1 2]

a = [1 1 1 2 3 4]
Output = 1

a = [1 2 2 3 3 4 4]
Output = [2 3 4]

Any ideas?

+4
source share
5 answers

You can do something like this:

a = [1 1 1 2 2 2 3 4];
values = [1 2 3 4];

counts = zeros(1,numel(values));

for i=1:numel(values)
    counts(i) = sum(a == values(i));
end

output = values(counts == max(counts));
+3
source

Alternative vector approach using histandunique

uVal = unique(a);
counts = hist(a,uVal);
out = uVal(counts == max(counts));

Results:

a = [1 1 1 2 2 2 3 4];

>> out

out =

 1     2
+5
source

, ( sort()), find, diff max -

%// Find starting indices of each island of identical numbers being
%// appended by the numel()+1 with the intention of getting island lengths
%// later on by differentiating along the indices
start_ind = [0 find(diff(a)) numel(a)]+1
lengths = diff(start_ind)

%// Look for the islands with the max island lengths. 
%// Use those to get unique numbers associated with them for final output
out = a(start_ind([lengths == max(lengths) false]))

, -

a = randi(10000,1,1000000);

disp('---------------- With for-loop')
tic
values = unique(a);
counts = zeros(1,numel(values));
for i=1:numel(values)
    counts(i) = sum(a == values(i));
end
output = values(counts == max(counts));
toc
clear output counts values

disp('---------------- With find+diff+max')
tic
sa = sort(a);
start_ind = [0 find(diff(sa)) numel(sa)]+1;
lengths = diff(start_ind);
out = sa(start_ind([lengths == max(lengths) false]));
toc
clear out lengths start_ind sa

disp('---------------- With mod')
tic
[~, ~, v] = mode(a);
result = v{1};
toc
clear v result

disp('---------------- With unique+hist+max')
tic
uVal = unique(a);
counts = hist(a,uVal);
out = uVal(counts == max(counts));
toc

Runtimes -

---------------- With for-loop
Elapsed time is 32.879074 seconds.
---------------- With find+diff+max
Elapsed time is 0.077948 seconds.
---------------- With mod
Elapsed time is 0.136005 seconds.
---------------- With unique+hist+max
Elapsed time is 0.250994 seconds.
+4

The third conclusion modegives just that. The input vector does not need to be sorted.

[~, ~, v] = mode(a);
result = v{1};
+4
source

You can also use accumarrayin combination with unique:

[vals,~,id] = unique(a);
b = accumarray(id, 1);
result = vals(b == max(b));
+2
source

All Articles