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

1 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 Entering Commands 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by