필터 지우기
필터 지우기

find unique rows of cell array

조회 수: 3 (최근 30일)
Matlab User
Matlab User 2016년 2월 25일
댓글: Matlab User 2016년 2월 25일
I have a cell array as attached. I can see that rows 3 and 4 are repeated, so I would like to keep only the first occurrence of this repetition. I have tried a few things, but calling unique(x, 'rows') doesn't work on cell arrays and each row is a different size, so i have found a few issues when trying to cell index.
Thankyou.

채택된 답변

Guillaume
Guillaume 2016년 2월 25일
편집: Guillaume 2016년 2월 25일
This is possibly more efficient than the other solutions:
[r1, r2] = ndgrid(1:size(matching__, 1));
duplicates = any(triu(arrayfun(@(r1, r2) isequal(matching__(r1, :), matching__(r2, :)), r1, r2), 1))
matching__(duplicates, :) = []
In your example, only rows 3 and 5 are identical.
  댓글 수: 1
Matlab User
Matlab User 2016년 2월 25일
Thankyou, this is great.

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

추가 답변 (2개)

Jos (10584)
Jos (10584) 2016년 2월 25일
편집: Jos (10584) 2016년 2월 25일
One, not so elegant option:
% A is your N-by-2 cell array holding arrays of doubles
% convert to strings
C = cellfun(@(x) sprintf('%.99f ',x),A,'un',0)
C = strcat(C(:,1),C(:,2)) ;
[~,k] = unique(C,'stable')
uniqueA = A(k,:)
Btw, I only see that rows 3 and 5 are the same ...
  댓글 수: 1
Matlab User
Matlab User 2016년 2월 25일
Thanks for your answer :)

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


Titus Edelhofer
Titus Edelhofer 2016년 2월 25일
Hi Matlab User,
that's probably not very easily solved. This should work:
i = 1;
while i<size(matching__, 1)
idx = cellfun(@(x) isequal(x, matching__{i,1}), matching__(i+1:end,1)) & cellfun(@(x) isequal(x, matching__{i,2}), matching__(i+1:end,2));
if any(idx)
matching__(find(idx)+i, :) = [];
end
i = i + 1;
end
although it's admittedly not very nice (and I hope your matching__ is not too huge).
Titus
  댓글 수: 1
Matlab User
Matlab User 2016년 2월 25일
Thankyou for your answer :)

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by