0

i have this number array

 A= [1  2   3   4
1   2   3   1
3   1   1   2
1   2   1   1
2   1   0   6
1   2   1   0]

i want to sort the 4th column from smallest to largest, and the corresponding rows will followed in their new position, something like this:

A =[1   2   1   0
1   2   3   1
1   2   1   1
3   1   1   2
1   2   3   4
2   1   0   6]

so the last row, become on the top because zero in the 4th column is the smallest number in 4th column, so how i will do that? thanks

nobel
  • 51
  • 1
  • 8

2 Answers2

3

You can do this:

[~,order] = sort(A(:,4));
A = A(order,:);
Wouter Kuijsters
  • 840
  • 6
  • 20
  • i tried it but it give an error: Indexing cannot yield multiple results. – nobel Mar 01 '15 at 12:56
  • @nobel: The code is correct, I don't know what's wrong in your case. Maybe you have a variable `sort` shadowing the function `sort` – Daniel Mar 01 '15 at 13:07
3

This will do:

sortrows(A,columnNumber);
Dandelion
  • 744
  • 2
  • 13
  • 34