I have the matrix as follows
a =
1 3
2 5
3 2
4 8
5 9
I want to sort the second column in the a
matrix. I want the corresponding rows of column one to be printed as follows :
a =
3 2
1 3
2 5
4 8
5 9
I tried sort(a)
, but it is sorting only the second column of matrix a
.
B = sort( A ) sorts the elements of A in ascending order. If A is a vector, then sort(A) sorts the vector elements. If A is a matrix, then sort(A) treats the columns of A as vectors and sorts each column.
Approach: Create a temp[] array of size n^2. Starting with the first row one by one copy the elements of the given matrix into temp[]. Sort temp[]. Now one by one copy the elements of temp[] back to the given matrix.
On the "Modeling" tab of the Data view, click the "Sort by Column" button and choose [Sort Order]. Go back to the Report view and add your matrix, adding row, column, and value fields. If the columns are sorted in the order you wanted, you win!
To access elements in a range of rows or columns, use the colon . For example, access the elements in the first through third row and the second through fourth column of A . An alternative way to compute r is to use the keyword end to specify the second column through the last column.
Try this:
sortrows(a,2)
This should sort according to the second column.
or use:
[val idx]=sort(a(:,2));
ans = [a(idx,1) val]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With