how to determine that all cell array value exists in other cell array?
47 views (last 30 days)
Show older comments
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 Comment
the cyclist
on 14 Jun 2023
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]};
Accepted Answer
the cyclist
on 14 Jun 2023
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)))
0 Comments
More Answers (1)
Samya
on 14 Jun 2023
Edited: Samya
on 14 Jun 2023
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!
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!