MATLAB: How to “partner” 2 matrixes

MATLAB

The matrix X is randomly shuffled, how does Y follow up that shuffle?
i.e:
X=[4 7 1 9;
3 0 6 8]
Y=[32 6 4 21;
77 89 0 2]
after the shuffle:
X=[3 9 4 6;
1 7 8 0]
Y=[77 21 32 0;
4 6 2 89]

Best Answer

X = [4 7 1 9;
3 0 6 8]
Y = [32 6 4 21;
77 89 0 2]
newX = [3 9 4 6;
1 7 8 0]
[found, where] = ismember(newX, X);
assert(all(found(:)), 'some elements of newX are not present in X')
newY = Y(where)