Filter löschen
Filter löschen

Generate a cell array with index values from two separate vectors

1 Ansicht (letzte 30 Tage)
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

Akzeptierte Antwort

Ameer Hamza
Ameer Hamza am 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 Kommentare
Ronald
Ronald am 24 Mai 2020
Thank you very much. This does exactly what I wanted to achieve!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Multidimensional Arrays finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by