Generate a cell array with index values from two separate vectors

1 次查看(过去 30 天)
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

采纳的回答

Ameer Hamza
Ameer Hamza 2020-5-24
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);

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by