Filter löschen
Filter löschen

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

4 Ansichten (letzte 30 Tage)
HAT
HAT am 12 Dez. 2021
Kommentiert: HAT am 13 Dez. 2021
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

Akzeptierte Antwort

Adam Danz
Adam Danz am 12 Dez. 2021
Bearbeitet: Adam Danz am 12 Dez. 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

Weitere Antworten (0)

Kategorien

Mehr zu Multidimensional Arrays finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by