Generate a cell array with index values from two separate vectors

1 view (last 30 days)
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

Accepted Answer

Ameer Hamza
Ameer Hamza on 24 May 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);

More Answers (0)

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!