Generate a cell array with index values from two separate vectors

1 vue (au cours des 30 derniers jours)
Ronald
Ronald le 24 Mai 2020
Commenté : Ameer Hamza le 25 Mai 2020
I have two cell arrays
x = {99983, 885735, 8993, 63742, 37443, 38437, 67638}
y = {99983, 885735, 99983, 885735, 8993, 63742, 37443, 38437, 67638, 8993, 99983, 885735, 8993, 63742, 99983, 885735, 8993, 63742, 37443, 38437, 67638,37443, 38437, 67638, 63742, 37443, 38437, 67638}
I would like to obtain a new cell array, z, such that:
z = {[1 3 11], [2 4 12 16], [5 11 13 17], [6 14 18 25], [7 19 22 26], [8 19 22 26], [9 21 24 28]}
In other words, z contains the index locations of the x elements in y

Réponse acceptée

Ameer Hamza
Ameer Hamza le 24 Mai 2020
Try this
x = {99983, 885735, 8993, 63742, 37443, 38437, 67638};
y = {99983, 885735, 99983, 885735, 8993, 63742, 37443, 38437, 67638, 8993, 99983, 885735, 8993, 63742, 99983, 885735, 8993, 63742, 37443, 38437, 67638,37443, 38437, 67638, 63742, 37443, 38437, 67638};
[~, idx] = ismember([y{:}], [x{:}]);
z = splitapply(@(x) {x}, 1:numel(y), idx);
  2 commentaires
Ronald
Ronald le 24 Mai 2020
Thank you very much. This does exactly what I wanted to achieve!
Ameer Hamza
Ameer Hamza le 25 Mai 2020
I am glad to be of help!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Multidimensional Arrays dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by