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