MATLAB: How to sort matrix based on comparison of two rows

sorting matrix

i have a matrix 1 2 3 4 5 2 5 6 3 9 4 5 7 8 4 i want to sort the matrix in a way that i want to compare row 2 and row 3 and elemnts of row 2 <row 3 should make a separate matrix and those which are greater makes a different matrix

Best Answer

It's not very clear what you want, possibly this:
m = [1 2 3 4 5
2 5 6 3 9
4 5 7 8 4]; %demo matrix
smaller = m(2, :) < m(3, :); %get a logical array indicating which elements in row 2 are smaller than row 3
msmaller = m(:, smaller); %use logical array to extract columns of m
mgreaterorequal = m(:, ~smaller); %negate logical array to extract the rest of m
Basically, use logical indexing to extract the part of the matrix that you want.