Sorting Arrays



Prev TutorialNext Tutorial

Create a script file and type the following code into it −
v = [ 23 45 12 9 5 0 19 17]  % horizontal vector
sort(v)                      % sorting v
m = [2 6 4; 5 3 9; 2 0 1]    % two dimensional array
sort(m, 1)                   % sorting m along the row
sort(m, 2)                   % sorting m along the column
When you run the file, it displays the following result −
v =
    23    45    12     9     5     0    19    17
ans =
     0     5     9    12    17    19    23    45
m =
     2     6     4
     5     3     9
     2     0     1
ans =
     2     0     1
     2     3     4
     5     6     9
ans =
     2     4     6
     3     5     9
     0     1     2

Cell Array

Cell arrays are arrays of indexed cells where each cell can store an array of a different dimensions and data types.
The cell function is used for creating a cell array. Syntax for the cell function is −
C = cell(dim)
C = cell(dim1,...,dimN)
D = cell(obj)

Where,

  • C is the cell array;
  • dim is a scalar integer or vector of integers that specifies the dimensions of cell array C;
  • dim1, ... , dimN are scalar integers that specify the dimensions of C;
  • obj is One of the following:
    • Java array or object
    • .NET array of type System.String or System.Object

Example

Create a script file and type the following code into it −
c = cell(2, 5);
c = {'Red', 'Blue', 'Green', 'Yellow', 'White'; 1 2 3 4 5}
When you run the file, it displays the following result −
c = 
{
  [1,1] = Red
  [2,1] =  1
  [1,2] = Blue
  [2,2] =  2
  [1,3] = Green
  [2,3] =  3
  [1,4] = Yellow
  [2,4] =  4
  [1,5] = White
  [2,5] =  5
}

Prev TutorialNext Tutorial