Sort Matrix according to another Vector

I have two sets of data such as:
A = [1 3 4 7 2]';
B = [3 4 7 1 2; 1 2 3 4 5; 6 7 8 9 10]';
Since B first column has the same values as A, but in different order, my question is if it is possible to sort B in a way that the resulting matrix is:
B = [1 3 4 7 2; 4 1 2 3 5; 9 6 7 8 10]';

 Accepted Answer

A = [1 3 4 7 2]';
B = [3 4 7 1 2; 1 2 3 4 5; 6 7 8 9 10]';
[~,idx] = ismember(A,B(:,1));
B = B(idx,:)
B = 5×3
1 4 9 3 1 6 4 2 7 7 3 8 2 5 10

More Answers (1)

A = [1 3 4 7 2]';
B_origin = [3 4 7 1 2; 1 2 3 4 5; 6 7 8 9 10]';
% Since B first column has the same values as A, but in different order,
% my question is if it is possible to sort B in a way that the resulting matrix is:
B_corrct = [1 3 4 7 2; 4 1 2 3 5; 9 6 7 8 10]';
[~,Locb] = ismember(A,B_origin(:,1))
Locb = 5×1
4 1 2 3 5
B_origin(Locb,:)
ans = 5×3
1 4 9 3 1 6 4 2 7 7 3 8 2 5 10
B_origin(Locb,:) == B_corrct
ans = 5×3 logical array
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

Categories

Community Treasure Hunt

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

Start Hunting!