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

6 ビュー (過去 30 日間)
Sim
Sim 2022 年 12 月 16 日
コメント済み: Sim 2022 年 12 月 16 日
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 Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by