MATLAB Answers

0

Find unique or duplicate cells in cell array of chars

Asked by Hadi Zyien on 29 Aug 2019
Latest activity Answered by Jos (10584)
on 29 Aug 2019
If I have a cell array of cells containing strings such as:
A = {{'A', 'B', 'C'}, {'C', 'D', 'E'}, {'A', 'B', 'C'}, {'C', 'B', 'A'}}
how can I find which cells are duplicates of each other? Or conversely, how can I find which cells are unique?
In this example, the duplicate cells are A(1) and A(3), while the rest are unique.
Thanks!

  0 Comments

Sign in to comment.

Products


Release

R2016b

1 Answer

Answer by Jos (10584)
on 29 Aug 2019
 Accepted Answer

A = {{'A', 'B', 'C'}, {'C', 'D', 'E'}, {'A', 'B', 'C'}, {'C', 'B', 'A'}}
N = arrayfun(@(k) sum(arrayfun(@(j) isequal(A{k}, A{j}), 1:numel(A))), 1:numel(A))
unique_elements = A(N==1)
duplicated_elements = A(N>1)

  0 Comments

Sign in to comment.