Removing duplicate rows (not "unique")

조회 수: 219(최근 30일)
Michael Siebold
Michael Siebold 2016년 5월 4일
댓글: saad sulaiman 2022년 11월 5일
I have a matrix with many (1e5+) rows and I want to remove both copies of all duplicate rows. Is there a fast way to do this? (This function needs to be run many times.)
  댓글 수: 4
Michael Siebold
Michael Siebold 2016년 5월 4일
Perfect and thanks a million! I kept messing with ia and ic, but just wasn't thinking histogram... Would you mind submitting this as an answer so I can accept it?

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

채택된 답변

Roger Stafford
Roger Stafford 2016년 5월 5일
편집: Roger Stafford 2016년 5월 5일
Let A be your matrix.
[B,ix] = sortrows(A);
f = find(diff([false;all(diff(B,1,1)==0,2);false])~=0);
s = ones(length(f)/2,1);
f1 = f(1:2:end-1); f2 = f(2:2:end);
t = cumsum(accumarray([f1;f2+1],[s;-s],[size(B,1)+1,1]));
A(ix(t(1:end-1)>0),:) = []; % <-- Corrected
  댓글 수: 6
saad sulaiman
saad sulaiman 2022년 11월 5일
how could we apply this code to a mesh where we have coordinate points for each triangle, such that we remove the internal edges, or edges shared by two triangles?
thanks in advance.

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

추가 답변(2개)

Azzi Abdelmalek
Azzi Abdelmalek 2016년 5월 4일
편집: Azzi Abdelmalek 2016년 5월 4일
The result
Elapsed time is 0.171778 seconds.
  댓글 수: 3
Mitsu 2021년 8월 3일
I reckon your answer does not address OP's question because running the following:
A=[1 1 1;1 1 1;1 1 0];
Will yield:
A = 1 1 0
1 1 1
Therefore, A still contains one instance of each row that was duplicate. I believe Michael wanted all instances of each row that appears multiple times be removed.

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

GeeTwo 2022년 8월 16일
%Here's a much cleaner way to do it with 2019a or later!
A_reduced=BG(B==1); % or just A if you want the results in the same variable.

Community Treasure Hunt

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

Start Hunting!

Translated by