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 ∖ V1∪V2∪V3 , 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'))
0 个评论
采纳的回答
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)')
% Determine which rows of matrix are unique
[~, ~, groupID] = unique(VAll,'rows','stable');
isUnq = histcounts(groupID,'BinMethod','integer') == 1;
% Return unique rows
w = VAll(isUnq,:)
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrices and Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!