Selecting numbers in matrix that are repeated

조회 수: 1 (최근 30일)
Cside
Cside 2019년 12월 1일
댓글: Star Strider 2019년 12월 1일
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
Star Strider 2019년 12월 1일
Try this:
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.
  댓글 수: 2
Cside
Cside 2019년 12월 1일
thank you! :) appreciate it
Star Strider
Star Strider 2019년 12월 1일
As always, my pleasure!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

KALYAN ACHARJYA
KALYAN ACHARJYA 2019년 12월 1일
[N,edges]=histcounts(A,unique(A))
result_array=edges(N>1)
  댓글 수: 1
Cside
Cside 2019년 12월 1일
there seems to be an error using histcounts (needs to be monotonically increasingly) - had a similar code with you too :) thank you nonetheless kalyan!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by