Find and remove equal element in 2 different cell with different size
2 次查看(过去 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))
0 个评论
采纳的回答
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))
New_A = A(~any(idx,2))
New_B = B(~any(idx,1))
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 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Axis Labels 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!