January 2

MATLAB Sort: Everything to Know

MATLAB Sort: Everything to Know

Tandose Sambo

What is MATLAB Sort?

MATLAB is all about matrices…and their manipulation.  “The Matrix” is an amazing world if embraced with the right attitude. Continuing with our central theme… let us delve into the world of Forest Gump. Forest Gump once said… “…Life is like a box of chocolates…”. Once you’re in a certain matrix frame of mind, you’ll start noticing that many things that we take for granted in life…are actually arrays.

Table of Contents (click to navigate)

Introduction to MATLAB Sort

MATLAB is all about matrices…and their manipulation.  “The Matrix” is an amazing world if embraced with the right attitude. Continuing with our central theme… let us delve into the world of Forest Gump. Forest Gump once said… “…Life is like a box of chocolates…”. Once you’re in certain matrix frame of mind, you’ll start noticing that many things that we take for granted in life…are actually arrays. Arrays, from our general discussions, are defined as a series of items that have been organized in a particular fashion. Taking a few seconds to analyze the box above, you’ll realize that the box of chocolates is arranged in an array – a 4×4 matrix. If you really take the time, you’ll identify certain patterns. The human brain is adroit at pattern recognition so trust yourself and take time to decipher the patterns. 

Dissecting the array above we see that:

  1. There are 6 white chocolates in the box
  2. There are 4 medium (milk) chocolates in the box
  3. There are 4 dark chocolates in the box

The order or chocolate arrangement has a specialized sequence to it. You’ll realize that:

  1. The top left to bottom right diagonal is purely white chocolates.
  2. The bottom left to top right diagonal is purely dark chocolates
  3. In every row and every column, there are alternating chocolates, with white chocolate themed chocolate as the primary alternate. The white chocolate is the mode of the entire batch. 

Most of the time, we really don’t process that mathematics is engrained in our lives…and that nature itself is an organized system of patterns. Such is the nature of arrays, and such is the nature of sorting objects. Mathematics is a descriptor of nature, and on a similar vein, objects can be organized in a fashion that enables them to be further manipulated. Mathematical modelling, via virtualization, enables various scenes to be visualized prior to actual physical execution. MATLAB Sort, enables us to achieve the function of sorting, via the use of the appropriate syntax.

What are the applications of Data Sorting?

Transitioning from the simple yet relevant example, data sorting is one of the most critical activities that any researcher or data analyst will encounter in their daily lives. When it comes to data, there are a myriad of ways that one can sort that data in order to achieve an appropriate objective. Data can be sorted in one or more of the following ways: 

  1. According to the actually received values. This data is what we call raw data received from the field.
  2. Counts of data. With available data, the frequency of certain parameters can be assessed. For example: How many times in a second does one blink?
  3. Percentages. When assessing systems, various percentages will be relevant to determining system behaviors. For example, what percentage of our customer base consists of millennials? 

The means of sorting data outlined here is by no means exhaustive, but it provides a means via which data analysts can begin to make sense of the data that they receive. Depending on your needs, you will go ahead and sort your data according to your predetermined variable value labels. With MATLAB or other statistical data tools such as SPSS, you can utilize background metadata, embedded into the system programs, to enable yourself to create value labels for your data sets. With this type of sorting, cleaner data organization can be facilitated, and ultimately, the appropriate conclusions can be reached.

The sorting function, though simple, eventually becomes more critical as larger and larger pools of data are analyzed. With large databases and research data, simply ensure that the appropriate system organizational plan is created so that all the fields are aligned in their appropriate domains.

What is MATLAB Sort?

MATLAB Sort is a tool in the MATLAB environment which enables various elements in fields, to be arranged in a pre-specified order. Under the MATLAB Struct, elements are arranged in order of their fields of their domain, as well as the associated variable attributes. This identifies whether they are numbers or letters, or a combination of both. With MATLAB Sort, the array elements are organized in patterns that facilitates order in the fields. When data is generated in reality, it exists as random variables. Envision collecting the age of persons who enter a bank in one day. At one point the person walking in might be a 25 year old wanting to use the ATM, at another point, it may a 65 year old wanting to cash a pension check they just received. 

With the random nature of numbers, data analysts want to ultimately organize the data in order to ensure that a representative set of silos of data can be collated, and assessed in the appropriate statistical fashion. MATLAB Syntax is highlighted below:

B = sort(A)

B = sort(A,dim)

B = sort(___,direction)

B = sort(___,Name,Value)

[B,I] = sort(___)

Let us now delve into how the functions operate on data sets: 

B = sort(A) is the function that will organize set elements in ascending order. 

B= sort (A, dim) is a function that utilizes specified dimensions in order to sort elements. A is the set that will be analyzed, and the dim function is the dimension that will be assessed. 

B = sort(___,direction)  is a function that sorts the elements of a pre-identified set, in a preferred direction. This direction could be in ascending or descending order.  

B = sort(___,Name,Value) is another means via which data can be sorted by prespecifying the criteria needed. 

 [B,I] = sort(___) is another means via which a collection of index vectors for a function can be collated. 

Brief Tutorial: MATLAB Sort 

With the theory described above, let us delve into the practice. Starting with a fresh command page, we will enter some data that will facilitate quantitative analysis.

From the MATLAB Workbook, we will enter the following data: 

Example One: Sorting of Vectors 

Vectors are quantities with both magnitude and direction. 

Objective: To create a row vector and sort its elements in ascending order.

A = [9 0 -7 5 3 8 -10 4 2];

B = sort(A)

The following is the MATLAB output after processing. Based on the data set, you can see that the data is appropriately organized in ascending order, from the smallest negative numbers to the largest positive number. 

Example Two: Matrix Organization

The next example involves organizing Matrix data. The MATLAB workbook outlines a 3×3 Matrix which is organized in a randomized order. The objective of this example is to organize the data in ascending order! Let’s do this! The following is the data that we will use in our analysis: 

A = [3 6 5; 7 -2 4; 1 0 -9]

A = 3 x 3

B = sort(A,2)
B = 3×3

Here’s an interesting point to note! You’ll probably encounter many error messages in MATLAB. In the process, you’ll encounter an error message as you proceed to enter the matrix data. This could be due to the program upgrade, as the code use is exactly as how MATLAB wants it. You can either ignore the error, since the system processes the data anyway, or you can actually make the new syntax changes as required. MATLAB seems to have preferences in the R2018b version, with respect to how the matrix data is processed. 

Example Three: Sorting Matrices in Descending Order 

In this example, the contents of a 4×4 matrix will be organized in descending order. Once again, entering our code: 

A = [10 -12 4 8; 6 -9 8 0; 2 3 11 -2; 1 1 9 3]

A = 4×4

From the syntax list above, the best instruction is the use of: 

B = sort(A,’descend’)

B = 4×4

The following is the system output: 

Example Four: Sorting Strings

From a data set in the MATLAB workbook, the following names in a field called A were supplied: 

A = [“Santos”,”Burns”; …

     “Jones”,”Morita”; …


B = sort(A)

Applying the MATLAB code yields the following organized array. Each column, in A, is now arranged in a manner that is alphabetized. MATLAB has its own algorithms that facilitate this change.

Sort the strings in each row. Applying the following code:

B = sort(A,2)

Will generate the appropriate data output highlighted below: 

With practice, you’ll continue the process and encounter a myriad of ways that you can utilize the MATLAB sort functions to organize your system data. 


Organizing data is a system process that involves arranging data, from a random array into a sequence. The sequence will make the data analysis process significantly easier to analyze and process. Once organized, the data can then be further understood. If there are gaps in the knowledge, the data can be organized into visual formats such as graphs. Research data often needs to be sorted in order to ensure that it can be comprehended. 

There is chaos in daily activity, but over time the data can further be organized in a fashion that enables the data to make sense. With MATLAB R2018b, the user can utilize the power of MATLAB in order to facilitate an appropriate organization and arrangement of your data. 

Reference Article: https://www.mathworks.com/help/matlab/ref/sort.html

Loved this? Spread the word

About the Author

Tandose Sambo is a Chemical Process Engineer, with a focus on improving process efficiency via operational improvements. Six-Sigma certified, and with a Design-focus and Data Analytics interest, she is a driven growing entrepreneur, with the intention to optimize industrial and business process operations. Her interests include sharing time with family and travelling.

Tandose Sambo

Related posts

10+ Best Construction Takeoff Software & Tools for Contractors in 2022

​Read More

Compare Autodesk Inventor vs Fusion 360: Review Which One to Buy

​Read More

How to Use Convert Entities Tool in SolidWorks: Beginner Tutorial

​Read More

[May 2020] Best Free CAD Design Software for Mac

​Read More
{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

Subscribe to our newsletter now!