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

1 vue (au cours des 30 derniers jours)
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))

Réponse acceptée

Turlough Hughes
Turlough Hughes le 8 Fév 2022
Modifié(e) : Turlough Hughes le 8 Fév 2022
% 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.

Plus de réponses (0)

Catégories

En savoir plus sur Entering Commands dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by