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

3 views (last 30 days)
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

Accepted Answer

Adam Danz
Adam Danz on 12 Dec 2021
Edited: Adam Danz on 12 Dec 2021
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

More Answers (0)

Categories

Find more on Multidimensional Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!