find unique rows of cell array
7 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
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.
更多回答(2 个)
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 ...
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
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrices and Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!