zaro

How to sort an array in Matlab?

Published in Matlab Array Operations 3 mins read

To sort an array in Matlab, you primarily use the built-in sort function.

The most common way to sort an array A in Matlab is by using the command B = sort( A ). This function sorts the elements of the input array A and returns the sorted array B.

Understanding the sort Function in Matlab

The sort function is versatile and adapts its behavior based on whether the input A is a vector or a matrix, among other array types.

  • Default Sorting Order: By default, sort uses ascending sorted order. This means elements are arranged from smallest to largest.
  • Sorting Vectors: If A is a vector (either a row or a column vector), then sort(A) sorts all the elements within that vector.
    • Example:
      A = [5, 2, 8, 1, 4];
      B = sort(A); % B will be [1, 2, 4, 5, 8]
  • Sorting Matrices: If A is a matrix, sort(A) treats each column of A as a separate vector and sorts each column independently.
    • Example:
      A = [5, 2;
           8, 1;
           4, 9];
      B = sort(A);
      % B will be:
      % [4, 1;
      %  5, 2;
      %  8, 9]
      % (Column 1 [5; 8; 4] is sorted to [4; 5; 8], and Column 2 [2; 1; 9] is sorted to [1; 2; 9])

Controlling Sorting Order and Dimension

While sort(A) defaults to ascending order and sorts columns in matrices, you can control these aspects using additional arguments.

Sorting in Descending Order

To sort in descending order (largest to smallest), you can specify the direction as 'descend'.

  • Syntax: B = sort(A, 'descend') or B = sort(A, dim, 'descend')
  • Example (Vector):
    A = [5, 2, 8, 1, 4];
    B = sort(A, 'descend'); % B will be [8, 5, 4, 2, 1]

Sorting Along a Specific Dimension

For matrices, you can specify the dimension along which to sort.

  • dim = 1 (default for matrices): Sorts down the rows (i.e., sorts each column).

  • dim = 2: Sorts across the columns (i.e., sorts each row).

  • Syntax: B = sort(A, dim)

  • Example (Sorting Rows):

    A = [5, 2;
         8, 1;
         4, 9];
    B = sort(A, 2); % Sorts along the second dimension (rows)
    % B will be:
    % [2, 5;
    %  1, 8;
    %  4, 9]
    % (Row 1 [5, 2] is sorted to [2, 5], Row 2 [8, 1] to [1, 8], etc.)

You can combine dimension and direction: B = sort(A, 2, 'descend') would sort each row in descending order.

Getting Sorting Indices

Often, you don't just want the sorted array, but also need to know the original positions of the elements in the sorted output. The sort function can return a second output: the index array.

  • Syntax: [B, I] = sort(A)
    • B is the sorted array.
    • I is an array of the same size as A (or B) containing the indices that sort A. B is constructed such that B(k) equals A(I(k)) for vectors, or B(r,c) equals A(I(r,c),c) when sorting columns of a matrix, and so on depending on the dimension.
  • Example (Vector with Indices):
    A = [5, 2, 8, 1, 4];
    [B, I] = sort(A);
    % B will be: [1, 2, 4, 5, 8]
    % I will be: [4, 2, 5, 1, 3]
    % (This means the 4th element of A (1) is first in B, the 2nd element of A (2) is second in B, etc.)

Summary Table

Here's a quick reference for common sort function usages:

Syntax Description Example (Vector A = [5, 2, 8, 1, 4]) Example (Matrix A = [5, 2; 8, 1])
sort(A) Sorts in ascending order (columns for matrices) [1, 2, 4, 5, 8] [5, 1; 8, 2] (Columns sorted)
sort(A, 'descend') Sorts in descending order (columns for matrices) [8, 5, 4, 2, 1] [8, 2; 5, 1] (Columns sorted)
sort(A, 1) Sorts along dimension 1 (columns) in ascending order Same as sort(A) Same as sort(A)
sort(A, 2) Sorts along dimension 2 (rows) in ascending order [1, 2, 4, 5, 8] (Vector is dim 1) [2, 5; 1, 8] (Rows sorted)
[B, I] = sort(A) Sorts and returns sorted array B and index array I B=[1..8], I=[4, 2, 5, 1, 3] B=[5,1;8,2], I=[1,2;2,1]

In summary, the fundamental command is sort(A), which sorts A in ascending order, treating matrix columns independently by default. You can add arguments for descending order or to sort along a specific dimension.