Search code examples
matlabcell-array

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'}.


Solution

  • 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