フィルターのクリア

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 Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by