Find least frequent value in an array
6 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hello,
Let's say I have an array:
H = [1 1 2 2 3 3 4 5 5 5]
and if I use mode(H) I would get 5 since it's the most frequent, but can I do the opposite and find the least frequent? i.e. 4 in this case
1 Kommentar
Dyuman Joshi
am 27 Sep. 2023
What if there are two (or more) values which occur the least frequent?
For e.g.
H = [1 2 3 1 2 2 3 4 3 3 5 3 5 6]
Here, 4 and 6 occur only once. What should be the output in here and in such cases?
Antworten (3)
Steven Lord
am 27 Sep. 2023
H = [1 1 2 2 3 3 4 5 5 5 6]
[counts, values] = histcounts(H, [unique(H) Inf])
allMinimumCountsLocations = counts == min(counts)
values(allMinimumCountsLocations)
I added Inf at the end of the list of unique values so that the last bin contained only the last value from unique(H) rather than the last two. If I had omitted it, the last bin would have contained both 5 and 6 (both of the last two edges) rather than just 6. This is not a bug; see the description of the edges input argument or the BinEdges name-value argument on the histcounts documentation page.
[counts, values, bins] = histcounts(H, unique(H))
valuesInLastBin = H(bins==max(bins))
0 Kommentare
Voss
am 27 Sep. 2023
Bearbeitet: Voss
am 27 Sep. 2023
H = [1 1 2 2 3 3 4 5 5 5];
[uu,ii] = unique(sort(H(:)));
[~,idx] = min(diff([ii; numel(H)+1]));
result = uu(idx)
% another example:
H = randi(5,10,10)
for i = 1:5
fprintf('%d appears %d times\n',i,nnz(H == i))
end
[uu,ii] = unique(sort(H(:)));
[~,idx] = min(diff([ii; numel(H)+1]));
result = uu(idx)
1 Kommentar
Voss
am 27 Sep. 2023
Note that in case more than one element of H appears the least often, this method returns the lowest valued one, just like mode() does with the most frequently appearing value.
H = [2 2 3 3 4 5 5 5 0 1 1 1];
mode(H) % returns 1, not 5
[uu,ii] = unique(sort(H(:)));
[~,idx] = min(diff([ii; numel(H)+1]));
result = uu(idx) % returns 0, not 4
But you can modify it:
[uu,ii] = unique(sort(H(:)));
d = diff([ii; numel(H)+1]);
result = uu(d == min(d)) % returns 0 and 4
And you can use a similar approach to get multiple most-frequent elements in case there are more than one:
[uu,ii] = unique(sort(H(:)));
d = diff([ii; numel(H)+1]);
result = uu(d == max(d)) % returns 1 and 5
Star Strider
am 27 Sep. 2023
Bearbeitet: Star Strider
am 27 Sep. 2023
One approach —
H = [1 1 2 2 3 3 4 5 5 5];
[Hu,~,uidx] = unique(H, 'stable');
[Out,idx] = min(accumarray(uidx,(1:numel(uidx)).', [], @(x)numel(H(x))));
Result = Hu(idx)
EDIT — (27 Sep 2023 at 17:02)
H = [1 2 3 1 2 2 3 4 3 3 5 3 5 6];
[Hu,~,uidx] = unique(H, 'stable');
Out = accumarray(uidx,(1:numel(uidx)).', [], @(x)numel(H(x)));
Result = Hu(find(Out == min(Out)))
.
0 Kommentare
Siehe auch
Kategorien
Mehr zu Environment and Settings finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!