Sort Matrix according to another Vector
Show older comments
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
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))
B_origin(Locb,:)
B_origin(Locb,:) == B_corrct
Categories
Find more on Shifting and Sorting Matrices 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!