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

24 visualizaciones (últimos 30 días)
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 comentario
the cyclist
the cyclist el 14 de Jun. de 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]};

Iniciar sesión para comentar.

Respuesta aceptada

the cyclist
the cyclist el 14 de Jun. de 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)))
allSinT = 3×1 logical array
1 1 1

Más respuestas (1)

Samya
Samya el 14 de Jun. de 2023
Editada: Samya el 14 de Jun. de 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!

Categorías

Más información sobre Matrices and Arrays en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by