This course will become read-only in the near future. Tell us at community.p2pu.org if that is a problem.

Session 4: Creating Matrices and Some Simple Matrix Operations


Objectives

Strength of Scilab, and the primary reason why we are learning how to use it is the fact that it operates on matrices just as easily as an ordinary calculator works with scalar data combined with the fact that all matrix operations are built-in and a host of functions for matrix operations are available. In this session we will learn the following:

  1. Creating matrices.
  2. Creating some frequently used standard matrices, such as zero matrices, identity matrices, diagonal matrices etc.
  3. Operations on matrices.

Creating Matrices

Here is how we create a matrix with 2 rows and 3 columns, with the first row containing the numbers 1, 2, 3 and the second row containing the numbers 4, 5, 6:

-->a = [1, 2, 3; 4, 5, 6] // a 2x3 matrix
 a  =
    1     2     3
    4     5     6
-->b = [1 2 3; 4 5 6] // Note, no commas, whitespace is same as comma
 b  =
    1     2     3
    4     5     6
-->c = [1 2 3
-->4 5 6]        // Same as above. Enter instead of semicolon
-->d = [1 2 3]   // Row vector, matrix of size 1x3
-->x = [1; 2; 3] // Column vector, matrix of size 3x1
-->y = [1 2 3]'  // Transpose. Row vector becomes column vector
-->z = 100       // Scalar, matrix of size 1x1. Check with whos -name z

Important Notes

  1. In Scilab, all constants are matrices. Even scalars are matrices of size 1x1.
  2. Elements in the row of a matrix can be separated by commas, whitespace or both.
  3. Rows of a matrix can be separated semicolon or newline or both.
  4. Elements of a matrix must be enclosed within a pair of matching square brackets.
  5. Number of columns in each row must be same. It is an error if you supply different number of columns in different rows. Thus, number of columns in subsequent rows must be the same as in the first row.

Screencast

Watch the screencast on YouTube (There is no audio. Read the comments. No need to type the comments when you try out the commands)

Size of a Matrix

You can enquire and determine the size of a matrix. The function are:

  • Function size() returns the size of a matrix as a row vector with two columns, the first column indicating the number of rows and the second column indicating the number of columns.
  • Function length() returns a scalar, the number of elements in the matrix.

-->a = rand(3, 4); size(a), length(a)
 ans =
    3.    4.
 ans =
    12.

Since matrix a has size 3x4, size() returns the row vector [3, 4] and length() returns the scalar 12.

Creating Frequently Used Standard Matrices

You can quickly create some frequently required standard matrices of the following types:

  1. Matrix with all elements zeros
  2. Matrix with all elements ones
  3. Identity matrix
  4. Matrix with specified values on the main diagonal
  5. Matrix with randomly generated numbers as its elements
-->a = zeros(3,4)     // Matrix of size 3x4, with all elements zeros
-->b = ones(4, 2)     // Matrix of size 4x2, with all element szeros
-->c = eye(3, 3)      // Identity matrix of size 3x3
-->d = eye(3, 5)      // Identity matrix of size 3x5, a non-square identity matrix!
-->x = diag([1 2 3])  // Diagonal matrix of size 3x3, diagonal elements given in the row matrix [1 2 3]
-->y = diag([1 2 3], 1)  // Matrix of size 4x4, matrix [1 2 3] placed one place above the main diagonal
-->z = diag([1 2 3], -1) // Same as above, but placed one place below the main diagonal
-->m = rand(3, 5)     // Matrix of size 3x5, with random numbers as its elements

Creating an Empty Matrix

An empty matrix is a matrix of size 0x0. Although not very useful in any calculations, it is possible to create an empty matrix, as follows:

-->x = [ ]        // Empty matrix
-->whos -name x   // Display size of matrix x

See what happens when you operate on an empty matrix. Try adding, subracting or multiplying an empty matrix to other non-empty matrices.

Screencast

Watch the screencast of YouTube (There is no audio. Read the comments. No need to type the comments when you try out the commands)

Matrix Operations

To start with, let us look at the following matrix operations:

  1. Matrix addition
  2. Matrix subtraction
  3. Matrix multiplications
  4. Matrix division
  5. Matrix transpose
  6. Exponentiation
  7. Arithmetic operations: sqrt(), abs() etc.
  8. Trigonometric operations
  9. Logarithmic operations
  10. Hyperbolic operations
-->// 3x3 matrix of random numbers between 0 and 1
-->a = rand(3, 3)
-->// 3x3 matrix of random numbers between 0 and 10
-->a = rand(3, 3) * 10
-->// 3x3 matrix of integer random numbers between 0 and 10
-->a = int(rand(3, 3)*10)
-->// b is transpose of a
-->b = a'
-->// c is the matrix sum of a and b
-->c = a + b
-->// d is matrix b subtracted from matrix a
-->d = a - b
-->// x is matrix product of a and b
-->x = a * b
-->// Create b, column vector of size 3x1, integer random numbers
-->b = int(rand(3, 1)*10)
-->// Solve linear equations [A] {x} = {b} for unknowns {x}
-->// written as x = a \ b, [a] in denominator, {b} in numerator
-->x = a \ b
-->// a^2 is same as a * a, matrix multiplication
-->x = a^2
-->// a^3 is same as a * a * a
-->y = a^3
-->// Matrix with elements which are square roots of corresponding elements of a
-->z = sqrt(a)
-->// Matrix with elements which are sine values of corresponding elements of a
-->y = sin(a)

Screencast

Watch the screencast on YouTube (There is no audio. Read the comments. No need to type the comments when you try out the commands)

Elementwise Operations

Some matrix operations, such as addition, subtraction, trigonometric, logarithmic, hyperbolic, sqrt(), abs() etc. are performed elementwise. But others, such as multiplication, exponentiation are not. Sometimes, it may be meaningful to perform these operations elementwise rather as matrix operations. For example, if you want c = a * b to mean an elementwise operation denoted by cij = aij * bij, j=1 to n, i=1 to m, then what you want is elementwise multiplication. In Scilab you can tell Scilab to perform an elementwise multiplication with the operator .*, as follows:

-->c = a .* b // a and b must be of same size
-->y = 1 ./ x // Invert each element of x and store it in. yij = 1 / xij

In the second statement above, space between the numerator 1 and decimal point (.) is necessary. 1./x is interpreted as 1.0 / x, which is invalid, unless x is of size 1x1. Similarly, a^2 is a*a, matrix multiplication of a and a. But b = a .* a is an elementwise operation so that bij = aij * aij, that is bij = (aij)^2.

-->a = int(rand(3,4)*10)
-->b = a.^2

Can you explain what is a .^3? Is a .^ 0.5 valid? If yes, what does it represent?

The operation a .+ b is an error, because matrix addition is already an elementwise operation.

Screencast

Watch the screencast on YouTube (There is no audio. Read the comments. No need to type the comments when you try ouy the commands)

Size of a Matrix

Scilab has functions to enquire and find the size of a matrix. The following functions are available:

  1. Function size() returns a row vector, with the columns of the row vector containing the number of rows and columns.
  2. Function length() returns the number of elements in the matrix.

-->a = ones(3, 4); b = ones(1, 5)l c = ones(4,1);
-->size(a), size(b), size(c)
 ans  =
    3.    4.
 ans  =
    1.    5.
 ans  =
    4.    1.
-->length(a), length(b), length(x)
 ans  =
   12.
 ans  =
    5.
 ans  =
    4.

Exercises

What is the output of the following expressions? Can you tell the sizes of the matrices? Assume zi, wn, wd, A and B in the third statement to be scalars and t to be a column vector.

  • a = rand(3,3) * 100 + 1
  • x = [0:%pi / 8: 2 * %pi]; y = sin(x)
  • y = exp(-zi * wn * t) .* (A * cos(wd * t) + B * sin(wd * t))

Task Discussion


  • Daniel Góngora   June 2, 2011, 12:26 p.m.

    Can you explain what is a .^3? Is a .^ 0.5 valid? If yes, what does it represent?

    a.^3 is just a*a*a performed elementwise. Where "a" represents a matrix.

    -->a = int(rand(3,4)*10)
     a  =
     
        6.    4.    1.    6.  
        8.    8.    5.    8.  
        4.    1.    5.    5.  
     
    -->a.^3
     ans  =
     
        216.    64.     1.      216.  
        512.    512.    125.    512.  
        64.     1.      125.    125.  
    

    a.^0.5 is indeed a valid expression, it represents the square root of every matrix element

    -->a.^0.5
     ans  =
     
        2.4494897    2.           1.          2.4494897  
        2.8284271    2.8284271    2.236068    2.8284271  
        2.           1.           2.236068    2.236068   
    

    Excercices

    What is the output of the following expressions?

    -->a = rand(3,3)*100+1
     a  =
     
        35.936154    95.881843    74.409406  
        39.737788    35.353372    27.157615  
        93.228987    38.601187    50.934938  
    
    -->x = [0 : %pi/8 : 2*%pi]; y = sin(x)
     y  =
     
     
             column 1 to 6
     
        0.    0.3826834    0.7071068    0.9238795    1.    0.9238795  
     
             column  7 to 11
     
        0.7071068    0.3826834    1.225D-16  - 0.3826834  - 0.7071068  
     
             column 12 to 16
     
      - 0.9238795  - 1.  - 0.9238795  - 0.7071068  - 0.3826834  
     
             column 17
     
      - 2.449D-16  
    
    -->zi = 1; wn = 0.3; wd = 2; A = 1; B = 0.5;
     
    
    -->t = [1 2 3 4 5 6 7 8]';
     
    
    -->y = exp(-zi * wn * t) .* (A * cos(wd*t) + B*sin(wd*t))
     y  =
     
        0.0285229  
      - 0.5663982  
        0.3335752  
        0.1051707  
      - 0.2479159  
        0.0951407  
        0.0773975  
      - 0.0999359  
    

    Can you tell the sizes of the matrices?

    -->size(a)
     ans  =
     
        3.    3.  
     
    
    -->size(x)
     ans  =
     
        1.    17.  
     
    
    -->size(y)
     ans  =
     
        8.    1.
    
  • Satish Annigeri   June 3, 2011, 1:04 a.m.
    In Reply To:   Daniel Góngora   June 2, 2011, 12:26 p.m.

    b = a .^ 3 can be understood as

    bij = (aij)^3

    Elementwise operation is an alternative operation when the corresponding matrix operation is not elementwise, for example, matrix multiplication or matrix raised to the power. Matrix addition and subtraction are by definition elementwise operations, and hence there is no separate matrix and elementwise operation for thes two operations.

    b = a .^0.5 is an elementwise square root of elements of a. Let us take an example:

    a = [4, 9, 16; 25, 36, 49]

    Then

    b = a .^ 3
    b = 
       64.    729.    4096.
    15625.  46656.  117649.

    a^3
       !--error  20

    c = a .^ 0.5
    c =
    2.  3.  4.
    5.  6.  7.

    d = sqrt(a)  // a.^0.5 and sqrt(a) are identical
    2.  3.  4.
    5.  6.  7.


    a = rand(3,3) * 100 + 1

    This is a statement. It is evaluated as follows:

    1. The expression on the right hand side (RHS) of the assignment operator is evaluated first and reduced to a single vale in a series of steps
    2. The value of the expression is assigned to the varibale a on the right

    Let us first see how the expression on the RHS is evaluated. It is evaluated from left to right.:

    1. The function rand(3,3) returns a 3x3 matrix containing random numbers as its elements. These random numbers are numbers between 0 and 1, with a normal distribution.
    2. The matrix of random numbers returned by rand(3,3) is now multiplied with the scalar 100, which means each element of that matrix is multipled with 100, thereby elements are now random numbers between 0 and 100
    3. To the matrix after multiplication with hundred we now add 1, which means we add 1 to each element of thematrix, thereby elements of this matrix are now random numbers between 1 and 101

    This resulting matrix is now stored in the variable a, which means that matrix a now is a 3x3 matrix consisting of random numbers between 1 and 101 as its elements.

    Note, each time you issue this command, you will get different numbers, because rand() generates random numbers.


    The size() function returns a matrix of size 1x2, that is a row vector with two columns. The first column represents the number of rows in the matrix and the second column represents the number of columns in the matrix. Therefore, in your above statements, matrix a has 3 rows and 3 columns (size 3x3), matrix x has 1 row and 17 columns (size 1x17, row vector) and y has 8 rows and 1 column (size 8x1, column vector)