how to determine that all cell array value exists in other cell array?

51 ビュー (過去 30 日間)
Mira le
Mira le 2023 年 6 月 14 日
コメント済み: Mira le 2023 年 6 月 15 日
Hello every one
I have a cell array
S=3×1 cell array
[ 29 56 62]
[34 40 62]
[48 52 7]
and other sequence of values in cell array
T{1} for examle:
1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74
Hiw can I check that all values in S are contain in T?
Thank you
  1 件のコメント
the cyclist
the cyclist 2023 年 6 月 14 日
To save anyone else who looks at this from the effort:
S = {[29 56 62];
[34 40 62];
[48 52 7]};
T = {[1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74]};

サインインしてコメントする。

採用された回答

the cyclist
the cyclist 2023 年 6 月 14 日
This does what I think you want:
S = {[29 56 62];
[34 40 62];
[48 52 7]};
T = {[1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74]};
allSinT = cellfun(@(x,y) all(ismember(x,y),2), S,repmat(T,size(S)))
allSinT = 3×1 logical array
1 1 1

その他の回答 (1 件)

Samya
Samya 2023 年 6 月 14 日
編集済み: Samya 2023 年 6 月 14 日
To check if all the values of S are contained in T, you can use the ismember function.
Here's the code to check if all values in S are contained in T{1} specifically:
all(ismember(S{1}, T{1}))
This will return a logical value of true if all the values in S{1} are contained in T{1} and false otherwise.
If you have multiple sequences in T and you need to check if all values in S are contained in any of them, you can use a loop like this:
all_included = false;
for i=1:length(T)
if all(ismember(S{i}, T{i}))
all_included = true;
break;
end
end
This will return a logical value of true in all_included if all the values in S are contained in any of the sequences in T, and false otherwise.
Hope this helps!
  1 件のコメント
Mira le
Mira le 2023 年 6 月 15 日
Thank you so much
that exectely what I want

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

製品


リリース

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by