Confounded
Confounded

Reputation: 522

Matlab unique rows cell array / table considering different permutations as equal

Given a cell array (or table) with, say, two columns, how to find unique permutations of values in the rows? That is, given A = {'a','b';'b','a';'c','d'}, what should be returned is {'a','b';'c','d'}.

Upvotes: 0

Views: 368

Answers (1)

Luis Mendo
Luis Mendo

Reputation: 112699

The fact that A is a cell array complicates things. You can do it this way:

[~, ~, u] = unique(A);        % get unique labels of cells
u = reshape(u,size(A));       % reshape into original shape
u = sort(u,2);                % sort each row
[~, r] = unique(u, 'rows');   % indices of unique rows
result = A(r,:);              % use those indices into input cell array

Upvotes: 2

Related Questions