MATLAB: How to get subset indices

indicessubset

I have M = [1 2 3 6 7 8]; and x = [3 7 8]; And I want to get indices [3 5 6], so M(indices) = x;

Best Answer

Use the ismember (or ismembertol for floating-point numbers):
M = [1 2 3 6 7 8];
x = [3 7 8];
[~,indices] = ismember(x,M);
Out = M(indices)
Out =
3 7 8