Rearrange cell array of strings based on occurrence in another cell array of string
6 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Cedric Kotitschke
el 18 de Abr. de 2023
Comentada: Cedric Kotitschke
el 18 de Abr. de 2023
Hey,
I have two cell arrays of string. Let's call them A and B:
A = {'test1', 'test2'};
B = {'here_test2_occurs', 'here_test1_occurs'};
I know that the elements of A occur in the elements of B. How can I reorder the cell array A so that the elements are in the same order as they occure in B?
Thanks!
0 comentarios
Respuesta aceptada
Stephen23
el 18 de Abr. de 2023
Editada: Stephen23
el 18 de Abr. de 2023
Assuming that every text in B contains exactly one text from A, and that every text in A occurs in B:
A = {'test1', 'test2', 'test1'};
B = {'here_test2_occurs', 'here_test1_occurs'};
F = @(a)find(contains(B,a));
[~,X] = sort(cellfun(F,A));
C = A(X)
Más respuestas (0)
Ver también
Categorías
Más información sobre Characters and Strings en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!