Compose-2020 Manipulate Matrices in OML

This tutorial explains how to:
  • Create a matrix.
  • Slice a matrix.
  • Extract a matrix.
  • Perform simple operations on matrices.

Creating a Matrix

Matrices are the backbone of OML and can hold floats, characters, and complex numbers.

  1. To create a matrix, use brackets [], as shown in the example below:
    a = [1,2;3,4]       

    You can also create the same matrix using the following commands:

    a = [1 2;3 4]
    a = [1 2
         3 4]
  2. Click Run. The three assignments return the following:
  3. For a complex matrix, use i in the matrix element or use a complex function:
    a = [1 2 ; 3+i 4+i]

    or

    a = complex([1 2;3 4],[0 0;1 1])
    Both will return this result:

Slicing a Matrix

  1. If you want to slice a whole row or column from that matrix, use : in the first column:
    a = [1,2;3,4]
    a(:,1)
    This extracts the first column of the matrix a:
  2. To slice several rows or columns from that matrix, use an array to specify the rows or columns:
    a = [1 2 3;4 5 6;7 8 9];
    % the first and third rows of a
    a([1, 3],:)

    This extracts the first and third rows of the matrix a:

  3. When slicing a matrix, end stands for the last index of a row or column.
    a = [1 2 3;4 5 6;7 8 9];
    a(end,:)

    This returns the last row of matrix a:

    a = [1 2 3;4 5 6;7 8 9];
    a(:,end-1)
    This script returns the second last column of a:

Extracting a Matrix

  1. If you want to extract a specific element in a matrix, you can use a(x,y) with a being the name of the matrix and x and y being the row and column being referenced, respectively. For example:
    a = [1,2;3,4]
    b = a(1,1)

    Results in:

  2. If you want to extract specific elements in a matrix, use two arrays to specify the rows and columns being referenced, respectively. For example:
    a = [1 2 3;4 5 6;7 8 9];
    a([1 3],[2 1])

    [1 3] stands for the first and third rows, [2 1] stands for the second and first column:

  3. A shortcut to extract the last elements in an array is to use end. See the example below:
    a = [1 2 3;4 5 6;7 8 9]
    b = a([2,3],[2:end])

    This results in:

  4. Assignment operations can be joined. Below is an example to extract an element from a sliced matrix:
    a = [1 2 3;4 5 6;7 8 9]
    % return to the third row of a
    a(3,:)
    % return to the second element of the third row
    a(3,:)(2)

Performing Operations on Matrices

  1. Most simple matrix operations can be done in the same way you perform operations on integers. To multiply two matrices of the same size, write the following:
    a = [1,2;3,4]
    b = [2,3;4,5]
    
    a*b

    The result is:

    You could use a.*b to perform entry-wise multiplication:

    Please refer to the Arithmetic Operation help document for more information on the other operations.

  2. To use the matrix in a built-in function, such as transpose, simply write:
    transpose([1,5,2;6,2,3])

    or, you could use .' for transpose, both return the same result:

    For more matrix operations, please refer to the script simple_matrix.oml, located in the Tutorials folder in the installation directory.