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

46 次查看(过去 30 天)
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!

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

产品


版本

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by