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 Center および File ExchangeMatrices and Arrays についてさらに検索

タグ

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by