This is the P2PU Archive. If you want the current site, go to www.p2pu.org!

Getting Started with Scilab

Session 4: Creating Matrices and Some Simple Matrix Operations

Satish Annigeri's picture
Sat, 2011-05-21 10:56

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 23 matrix
 a  =
    1     2     3
    4     5     6
-->b = [1 2 3; 4 5 6] // Same as above. 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 of a row vector of size 1x3 becomes matrix of size 3x1
-->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.

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, strange to see 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

---

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
-->a = int(rand(3, 3)*10)  // Matrix of size 3x3, containing integer random numbers betwwen 0 and 10
-->b = a'        // Transpose a, and place it in b
-->c = a + b    // Add a and b, and place the result in c
-->d = a - b    // Subtraction
-->x = a * b    // Multiplication
-->b = int(rand(3, 1)*10) // Redfine b for the next operation
-->x = a \ b    // Solution of linear equations [A] {x} = {b}.
                // Written as {x} = {b} / {a}. Note, [a] in denominator
-->x = a^2      // Same as x = a * a
-->y = a^3      // Same as y = a * a * a
-->z = sqrt(a)  // Matrix with elements which are square roots of corresponding elements of a
-->x = abs(a)   // Matrix with elements which are absolute values of corresponding elements of a
-->y = sin(a)   // Matrix with elements which are sine values of corresponding elements of a

---

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.