필터 지우기
필터 지우기

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

조회 수: 4 (최근 30일)
HAT
HAT 2021년 12월 12일
댓글: HAT 2021년 12월 13일
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에서 Multidimensional Arrays에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by