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

조회 수: 3 (최근 30일)
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
Sim
Sim 2022년 12월 16일
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);

댓글을 달려면 로그인하십시오.

채택된 답변

Matt J
Matt J 2022년 12월 16일
편집: Matt J 2022년 12월 16일
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
Sim
Sim 2022년 12월 16일
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]}

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by