How to sort matrix based on comparison of two rows?

1 view (last 30 days)
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

Accepted Answer

Guillaume
Guillaume on 24 Dec 2014
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.

More Answers (0)

Categories

Find more on Matrices and Arrays 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!