How to get a complement of a cell array in matlab?

7 次查看(过去 30 天)
I would like to write a matlab code to get a cell array W so that W is "V without the union of three different arrays V1, V2 and V3", i.e., W = V V1V2V3 , where V= {[1,2], [3,5], [5,6], [2,4],[3,7], [3,9], [8,9] }, V1 = {[1,2], [3,5], [5,6]}, V2 = {[2,4],[3,7] }, and V3= {[3,9] }.
I have tried the following, but it yields only number W=8. But I wanted to get W = { [8,9]}.
Please any hint/assistance? Thanks in advance!
V= {[1,2], [3,5], [5,6], [2,4],[3,7], [3,9], [8,9] };
V1 = {[1,2], [3,5], [5,6]};
V2 = {[2,4],[3,7] };
V3= {[3,9] };
W = setdiff(cell2mat(V(:)), union(union(cell2mat(V1(:)),cell2mat(V2(:)),'rows'),cell2mat(V3(:)),'rows'))
W = 8

采纳的回答

Adam Danz
Adam Danz 2021-12-12
编辑:Adam Danz 2021-12-12
This solution works if all elements of the 1D cell arrays 1x2 row vectors.
V = {[1,2], [3,5], [5,6], [2,4],[3,7], [3,9], [8,9] };
V1 = {[1,2], [3,5], [5,6]};
V2 = {[2,4],[3,7]};
V3= {[3,9]};
% Combine all cell arrays into an mx2 matrix
VAll = cell2mat(horzcat(V,V1,V2,V3)')
VAll = 13×2
1 2 3 5 5 6 2 4 3 7 3 9 8 9 1 2 3 5 5 6
% Determine which rows of matrix are unique
[~, ~, groupID] = unique(VAll,'rows','stable');
isUnq = histcounts(groupID,'BinMethod','integer') == 1;
% Return unique rows
w = VAll(isUnq,:)
w = 1×2
8 9

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by