Home » All Programs » Sorting Elements Of A Matrix Or Vector In Ascending And Descending Way On MatLab

Sorting Elements Of A Matrix Or Vector In Ascending And Descending Way On MatLab

Matrices and vectors can be used for data collection from user, and these numerical datas can be contained inside matrices and vectors in Matlab. Or, coder can enter the numerical datas inside matrices and vectors. If the sorting of these elements inside matrices and vectors are needed, you can use ‘sort()’ command in Matlab. In here, we explain how to use ‘sort()’ command to sort elements according to their values, both in ascending and descending directions.

How To Use ‘sort()’ Command In MatLab?

>> a = [2 6 3; 4 6 3; 11 69 31];
b = [2 65 2 9 46 12 38];
sort(a,1)
 %the first
sort(a,2)
 %the second
sort(b,'ascend')
 %the third
sort(b,'descend')
 %the fourth
sort(a,2,'descend')
 %the fifth

ans =

     2     6     3
     4     6     3
    11    69    31


ans =

     2     3     6
     3     4     6
    11    31    69


ans =

     2     2     9    12    38    46    65


ans =

    65    46    38    12     9     2     2


ans =

     6     3     2
     6     4     3
    69    31    11

We explain the sort command in 5 use of it as you see above. There are bunch of codes are wrote in Matlan command window, and some answers are obtained as you see above.

YOU CAN LEARN MatLab IN MECHANICAL BASE; Click And Start To Learn MatLab!

The matrix ‘a’ and the vector ‘b’ are obtained first. At the first use of sort() command, the elements of matrix ‘a’ is sorted from smaller to bigger values in each columns. The ‘1’ means, sorting of elements will be done in each columns. Take a look at the first answer in command window.

In second use of ‘sort()’ command above, we typed ‘2’ instead of ‘1’. So the sorting of elements is done for rows for matrix ‘a’ in Matlab. Take a look at the second answer appeared in command window to check it.

In third use, we used the vector ‘b’ and typed ‘ascend’. The use is straightforward that all of matrix ‘b’ sorted in ascending way. Check it out in the third answer appeared on command window in Matlab.

In the fourth use of ‘sort()’ command above, we just typed ‘descend’ instead of ‘ascend’. So, this time elements of vector ‘b’ is sorted in the way of ascending direction. Check the fourth answer to see the result.

The ‘ascend’ and ‘descend’ logic is valid for matrices also. In the last example of ‘sort()’ command above, we sort the rows(typed 2) of matrix ‘a’, in the descending direction. Take a look to the fifth answer appeared again in command window in Matlab.

Here is the use of ‘sort()’ command in very simple in Matlab to sort the elements of matrices and vectors. Do not forget to leave your comments and questions about ‘sort()’ command in Matlab below.

Your precious feedbacks are very important for us.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *


Please

leave feedback ?

( For the post )

ASK THE MECHANICALLAND

COMMUNITY


We are always open to your feedback to improve ourselves and the quality of our content! If you have any suggestions, thoughts, or criticism, please let us know. We are trying to improve our blog with constructive feedback. We are aware of how valuable your feedback is for our future development, and we will carefully read all your comments. Thank you in advance!