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 builtin and a host of functions for matrix operations are available. In this session we will learn the following:

Creating matrices.

Creating some frequently used standard matrices, such as zero matrices, identity matrices, diagonal matrices etc.

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

In Scilab, all constants are matrices. Even scalars are matrices of size 1x1.

Elements in the row of a matrix can be separated by commas, whitespace or both.

Rows of a matrix can be separated semicolon or newline or both.

Elements of a matrix must be enclosed within a pair of matching square brackets.

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:

Matrix with all elements zeros

Matrix with all elements ones

Identity matrix

Matrix with specified values on the main diagonal

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 nonsquare 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 nonempty 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:

Matrix addition

Matrix subtraction

Matrix multiplications

Matrix division

Matrix transpose

Exponentiation

Arithmetic operations: sqrt(), abs() etc.

Trigonometric operations

Logarithmic operations

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 = a_{ij} * b_{ij}, 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 b_{ij} = a_{ij} * a_{ij}, that is b_{ij} = (a_{ij})^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:

Function size() returns a row vector, with the columns of the row vector containing the number of rows and columns.

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))