Selecting numbers in matrix that are repeated
5 views (last 30 days)
I have a 60x5 matrix A and would like to find the answer vector that lists all the numbers in A that appeared at least 2 times/remove those numbers that only appeared once. How may I write for this?
(A contains some NaNs too)
Thank you! :)
Star Strider on 1 Dec 2019
A = randi(1000, 60, 5); % Create Matrix
[Au,~,ix] = unique(A,'stable'); % Unique elements in ‘A’
tally = accumarray(ix,1); % Number Of Occurrences Of Each
keep = tally > 1; % Keep Indices Of Occurrences > 1
out1 = [Au(keep), tally(keep)]; % Display Numbers & Occurrences
Ai = ismember(A,out1(:,1)); % Return Logical Index Of Occurrences > 1
Anew = A .* Ai; % Matrix With ‘Single-Occurrence’ Elements Eliminated
The ‘Anew’ matrix has 0 values for the elements that are not repeated, those that are repeated appear in their expected locations.
If you only want the values of the numbers that aare repeated, and the number of repeats, those are contained in the ‘out1’ matrix.