William
William

Reputation: 43

How to sort specific rows in matlab

I have a 15x100 matrix and I only want to sort the the first 10 rows in ascending order, what matlab code would do that?

edit: Sort the first 10 rows of each column

Upvotes: 2

Views: 1590

Answers (1)

Dan
Dan

Reputation: 45741

x(1:10, :) = sortrows(x(1:10, :), 1:size(x,2));

The second argument of sortrows tells it which columns in which order you want to sort. so 1:size(x, 2) will sort by each column in turn (in ascending order)

If you actually want all the columns to be perfectly sorted (rows 1 to 10) and not keep the row integrity (i.e. each row can no longer be found in the original) then (although this is weird):

for col = 1:size(B, 2)
    B(1:10, col) = sort(B(1:10, col));
end

Upvotes: 7

Related Questions