Find repeated values in a matrix

조회 수: 2 (최근 30일)
Yro
Yro 2020년 12월 9일
댓글: Walter Roberson 2020년 12월 9일
How can I find repeated values at the same time in two columns of an array (12x2):
A = [26 24
28 35
31 34
33 31
33 31
33 28
35 25
31 26
30 26
28 29
27 30
26 32];
In A the [33 31] is repeated. I tried to use the unique function but it only finds the unique values in a column.
[C, ia, ic] = unique(A(:,1), 'rows');
Thanks in advance.

답변 (1개)

Walter Roberson
Walter Roberson 2020년 12월 9일
A = [26 24
28 35
31 34
33 31
33 31
33 28
35 25
31 26
30 26
28 29
27 30
26 32];
[C, ~, G] = unique(A, 'rows');
counts = accumarray(G, 1);
mask = counts > 1;
[counts(mask), C(mask,:)]
ans = 1×3
2 33 31
  댓글 수: 2
Yro
Yro 2020년 12월 9일
Thanks a lot, it works. One more question, if I want to know if some value is repeated or not?
Walter Roberson
Walter Roberson 2020년 12월 9일
[wasfound, idx] = ismember(ThingToFind, A, 'rows');
repcounts = zeros(size(wasfound));
repcounts(wasfound) = counts(idx(wasfound));
[repcounts, ThingToFind]
Here, ThingToFind may be a 2D array with the same number of columns as A has, and the associated count will be calculated for each entry.

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

카테고리

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