Get supersets from cell array of doubles

조회 수: 2 (최근 30일)
Hau Kit Yong
Hau Kit Yong 2021년 1월 20일
편집: dpb 2021년 1월 21일
I have a cell array of doubles, e.g.
C{1} = [1,2,3,4];
C{2} = [3,4,5,6,7,8];
C{3} = [2,3];
C{4} = [7,8,9,10];
C{5} = [1,2,6,7,8,9,10];
I want to find the indexes of arrays that are not fully contained within another array, so in this case, the code should return
[1,1,0,0,1]
I reckon some use of ismember is needed but I can't quite make out how.
  댓글 수: 1
dpb
dpb 2021년 1월 21일
편집: dpb 2021년 1월 21일
I think you can only do that by testing each set with the combination of all other sets together.
ADDENDUM:
On thinking about it, I believe I'd be tempted to put the whole mess into a 2D array with the data in one column and the cell number in the second. Then a grouping variable could be used to operate by what is now cell.

댓글을 달려면 로그인하십시오.

채택된 답변

dpb
dpb 2021년 1월 21일
편집: dpb 2021년 1월 21일
I misread the problem statement originally and the second idea didn't really help all that much. It's fairly straightforward, though...
res=false(numel(C));
for i=1:numel(C)
isOK=false;
for j=setdiff(1:numel(C),i)
isOK=isOK|all(ismember(C{i},C{j}));
end
res(i)=~isOK;
end
For the sample array above produces
>> res
res =
1×5 logical array
0 0 1 1 0
>> res=~res
res =
1×5 logical array
1 1 0 0 1
>>
before incorporating the not operator inside the loop over i ...did it that way to debug more easily. One could change the sense of the return from all at that point instead; your call.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

태그

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by