find unique rows of cell array

7 views (last 30 days)
Matlab User
Matlab User on 25 Feb 2016
Commented: Matlab User on 25 Feb 2016
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.

Accepted Answer

Guillaume
Guillaume on 25 Feb 2016
Edited: Guillaume on 25 Feb 2016
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.

More Answers (2)

Jos (10584)
Jos (10584) on 25 Feb 2016
Edited: Jos (10584) on 25 Feb 2016
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
Titus Edelhofer on 25 Feb 2016
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

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!