Filter löschen
Filter löschen

find unique rows of cell array

3 Ansichten (letzte 30 Tage)
Matlab User
Matlab User am 25 Feb. 2016
Kommentiert: Matlab User am 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.

Akzeptierte Antwort

Guillaume
Guillaume am 25 Feb. 2016
Bearbeitet: Guillaume am 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.

Weitere Antworten (2)

Jos (10584)
Jos (10584) am 25 Feb. 2016
Bearbeitet: Jos (10584) am 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 am 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

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by