A simple way to find (in a cell array) rows that, once flipped, are duplicates of other rows?

3 views (last 30 days)
A simple way to find (in a cell array) rows that, once flipped, are duplicates of other rows?
E.g. in this example
a = [
{[ 28 27]}
{[ 28 16]}
{[ 14 17 8]}
{[ 14 12 25]}
{[ 14 11]}
{[ 8 17 14]}
{[ 8 17 12 25]}
{[ 27 28]}
{[ 27 30]}
{[ 16 28]}
{[16 19 3 7 9 24]}
{[24 9 7 3 19 16]}
{[ 25 12 14]}
{[ 25 12 17 8]}
{[ 11 14]}
{[ 30 27]}
{[ 29 18 14]}
{[ 29 3 19 16]}
{[ 29 7 9 24]}
{[ 5 1]}
{[ 5 19 16]}
{[ 5 19 3 7 9 24]}
{[ 21 1]}
{[ 21 23]}]
the 8th row,
{[ 27 28]}
once flipped, is duplicate of the first row
{[ 28 27]}
  1 Comment
Sim
Sim on 16 Dec 2022
An unsuccessful attempt:
a = [
{[ 28 27]}
{[ 28 16]}
{[ 14 17 8]}
{[ 14 12 25]}
{[ 14 11]}
{[ 8 17 14]}
{[ 8 17 12 25]}
{[ 27 28]}
{[ 27 30]}
{[ 16 28]}
{[16 19 3 7 9 24]}
{[24 9 7 3 19 16]}
{[ 25 12 14]}
{[ 25 12 17 8]}
{[ 11 14]}
{[ 30 27]}
{[ 29 18 14]}
{[ 29 3 19 16]}
{[ 29 7 9 24]}
{[ 5 1]}
{[ 5 19 16]}
{[ 5 19 3 7 9 24]}
{[ 21 1]}
{[ 21 23]}];
a_flipped = cellfun(@fliplr,a,'un',0);
ismember(a,a_flipped,'rows')
Warning: The 'rows' input is not supported for cell array inputs.
Error using cell/ismember
Input A of class cell and input B of class cell must be cell arrays of character vectors, unless one is a character vector.

Error in cellismemberlegacy (line 53)
[lia,locb] = ismember(a,b);

Error in cell/ismember (line 65)
lia = cellismemberlegacy(a,b,flag1);

Sign in to comment.

Accepted Answer

Matt J
Matt J on 16 Dec 2022
Edited: Matt J on 16 Dec 2022
n=numel(a);
map=false(n);
for i=1:n
for j=i:n
map(i,j)=isequal(a{i},flip(a{j}));
end
end
[I,J]=find(map) %matches
I = 8×1
3 1 2 11 4 7 5 9
J = 8×1
6 8 10 12 13 14 15 16
  1 Comment
Sim
Sim on 16 Dec 2022
Thanks a lot @Matt J!! :-)
Thanks for finding the flipped duplicates:
>> a(J)
ans =
8×1 cell array
{[ 8 17 14]}
{[ 27 28]}
{[ 16 28]}
{[24 9 7 3 19 16]}
{[ 25 12 14]}
{[ 25 12 17 8]}
{[ 11 14]}
{[ 30 27]}

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!