Find and remove equal element in 2 different cell with different size

2 ビュー (過去 30 日間)
NA
NA 2022 年 2 月 8 日
編集済み: Turlough Hughes 2022 年 2 月 8 日
I have cell A and B.
A = {[100,103,104],[4,5,11],[66],[4,5,1],[85,88,89,77]};
B = {[40,41,41],[4,5,11],[68],[85,88,89,77],[31,66],[1,9,8,7,5],[100,103,104]};
I want to find equal cell in A and B and then remove it form both cell.
Result should be
same_cell = {[100,103,104],[4,5,11],[85,88,89,77]}
New_A = {[4,5,11],[66],[4,5,1]}
New_B = {[40,41,41],[68],[31,66],[1,9,8,7,5]}
I tried isequal but A and B are different in size.
find(cellfun(@isequal, A, B))

採用された回答

Turlough Hughes
Turlough Hughes 2022 年 2 月 8 日
編集済み: Turlough Hughes 2022 年 2 月 8 日
% Your sample data
A = {[100,103,104],[4,5,11],[66],[4,5,1],[85,88,89,77]};
B = {[40,41,41],[4,5,11],[68],[85,88,89,77],[31,66],[1,9,8,7,5],[100,103,104]};
You could do the following
idx = cellfun(@(a) cellfun(@(b) isequal(a,b),B), A,'uni',0);
idx = vertcat(idx{:});
same_cell = A(any(idx,2))
same_cell = 1×3 cell array
{[100 103 104]} {[4 5 11]} {[85 88 89 77]}
New_A = A(~any(idx,2))
New_B = 1×2 cell array
{[66]} {[4 5 1]}
New_B = B(~any(idx,1))
New_A = 1×4 cell array
{[40 41 41]} {[68]} {[31 66]} {[1 9 8 7 5]}
The rows in idx correspond to A and the columns in idx correspond to B. More specifically, rows in idx which contain a 1 corresponds to a cell in A which matched with B, so you can index A(~any(idx,2)) to obtain the non matching cells. Similarly, columns in idx which contain a 1 correspond to a cell in B which matched with A, hence B(~any(idx,1)) gives the non matching cells in B.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeEntering Commands についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by