How to find duplicate values in 2d matrix?

조회 수: 5 (최근 30일)
Javier
Javier 2019년 7월 26일
편집: Rik 2019년 7월 26일
Hello,
I have the following matrix
M =[ 1540 1685 6.90000000000000 24.2000000000000 14.6000000000000;
1540 1700 9.90000000000000 15.5000000000000 17.9000000000000;
1540 1701 9.90000000000000 12.4000000000000 17.9000000000000;
1540 1685 6.90000000000000 23.5000000000000 14.600000000000
1540 1700 9.90000000000000 15 17.9000000000000;
1540 1701 9.90000000000000 13.3000000000000 17.9000000000000;
1540 1680 6.90000000000000 26.2000000000000 14.4000000000000;
1540 1689 6.90000000000000 26.1000000000000 14.6000000000000;
1540 1680 6.90000000000000 24 14.6000000000000;
1540 1680 6.90000000000000 24 14.5000000000000;
1540 1688 7 23.5000000000000 14.6000000000000;
]
And I would like to identify the duplicates of that matrix based on the first two colums and extract them to furher operations. Thus, I would like to have a way to get a sub-matrices of the form
M11 = [ 1540 1700 9.90000000000000 15.5000000000000 17.9000000000000;
1540 1700 9.90000000000000 15 17.9000000000000;
]
M22 =[ 1540 1680 6.90000000000000 26.2000000000000 14.4000000000000;
1540 1680 6.90000000000000 24 14.5000000000000;
]
How can I achieve this? Could I use findgroups for this purpose?
Thanks in advance
  댓글 수: 2
KSSV
KSSV 2019년 7월 26일
Read about unique
Jan
Jan 2019년 7월 26일
This is a 2D matrix, not 4D.

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

채택된 답변

Rik
Rik 2019년 7월 26일
편집: Rik 2019년 7월 26일
You can indeed use findgroups. You should supply the first two columns as separate inputs.
G=findgroups(M(:,1),M(:,2));
Then I would suggest looping through the group IDs and fill a cell array. Don't use numbered variables, because they are hard to use in further analysis.
  댓글 수: 2
Javier
Javier 2019년 7월 26일
Thanks Rik,
To be sure that I understand the output of findgroups, G variable contains group identifiers right? Meaning that for instance all elements with same M(:,1) and M(:,2) share same identifier, is that correct?
Then I could do something like
for k=1:max(G)
idx = G == k;
subM{k} = [M(idx,1) M(idx,2) M(idx,3) M(idx,4) M(idx,5)]
end
and I could check for the differences?
Rik
Rik 2019년 7월 26일
편집: Rik 2019년 7월 26일
That should work. You can also use ismember to generate the idx vector (in which case that would become a logical vector). You can also more generally use M(idx,:) instead of specifying each column separately.
Also, starting off with subM=cell(1,max(G)); is probably a good idea.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by