MATLAB: How to sort matrix based on another matrix sort matrix A=[3,2,1;8,3,1;6,4,2] and B=[5,9,2;9,2,1;8,7,4] Matrix A sort by row like A=[1,2,3;1,3,8;2,4,6] then based on A change values of B matrix like B=[2,9,5;1,9,2;4,7,8]. Best Answer >> A = [3,2,1;8,3,1;6,4,2]A = 3 2 1 8 3 1 6 4 2>> B = [5,9,2;9,2,1;8,7,4]B = 5 9 2 9 2 1 8 7 4>> [A,X] = sort(A,2);>> for k = 1:size(X,2), B(k,:) = B(k,X(k,:)); end>> AA = 1 2 3 1 3 8 2 4 6>> BB = 2 9 5 1 2 9 4 7 8 Related SolutionsMATLAB: How to find a subset matrix in the main matrix and build a new matrix I assume that the order of the rows in C don't matter:[~,~, ib] = intersect(A, B(:, 1:5), 'rows'); %find rows of A in Bids = B(ib, 6); %get the ID of those rowsC = B(ismember(B(:, 6), ids), :); %get rows whose ID match MATLAB: Create a new array from an intersection of two previous arrays >> A = [1 1 1 1 4 4 4 4 6 6 6 6 6 6];>> B = [2 2 2 2 2 2 2 2 2 2 7 7 7 7 7 7 7 7 8 8 8 8 9 9 9 9];>> N = min(numel(A),numel(B));>> C = [reshape([A(1:N);B(1:N)],1,2*N),A(N+1:end),B(N+1:end)]C = 1 2 1 2 1 2 1 2 4 2 4 2 4 2 4 2 6 2 6 2 6 7 6 7 6 7 6 7 7 7 7 7 8 8 8 8 9 9 9 9 Related QuestionHelp running Matlab scriptFind missing element between two matrixHow to fit a series of datapoints using 3 piecewise linear fit linesFind how many times same element is repeatedIs it possible to extract the values with a vector of indices for each row without using the for statement from the matrix
Best Answer