Create two matrices using the following code:
A = [9, 4, 7; 1, 11, 3]; B = [4, 15, 6, 8, 10, 1, 2, 9, 5, 7, 14, 3]; B = reshape(B,2,3,2);
A = 9 4 7 1 11 3
B(:,:,1) = 4 6 10 15 8 1 B(:,:,2) = 2 5 14 9 7 3
Apply the following procedure to get the elements of a matrix in ascending order:
% Elements of A in ascending order resultExample1 = sort(A(:));
resultExample1 = 1 3 4 7 9 11
% Elements of B in ascending order resultExample2 = sort(B(:));
resultExample2 = 1 2 3 4 5 6 7 8 9 10 14 15
I recommend the following books to learn more on matrix sorting in MATLAB.
This book was the winner of a 2017 Textbook Excellence Award. Chapter 14 covers matrix sorting, curve fitting, complex numbers, integration, differentiation, and other topics.
MATLAB for Engineers (5th Edition)
This excellent book is especially oriented to engineers and scientists who want to learn MATLAB programming. The third chapter covers matrix sorting and describes several data analysis functions, among other topics.
I also recommend the other tutorials in this series.
- How to Sort a Vector in MATLAB
- How to Sort a 2D Matrix in MATLAB
- How to Sort a Multidimensional Matrix in MATLAB
- How to Sort a 2D Matrix by Column in MATLAB
- How to Sort a 2D Matrix by Row in MATLAB
- How to Get the Elements of a Matrix in Ascending Order in MATLAB
- How to Get the Elements of a Matrix in Descending Order in MATLAB
I hope you have learned how to get the elements of a matrix in ascending order in MATLAB. The source code developed in this tutorial is available at this page.