Matrix Constructors, Operations and Analysis in Scilab

In Scilab, everything is a matrix. In this post, we will discuss how to create matrices, how to analyze matrices, and how to access elements of a matrix. Here, the basic data type is the matrix, which is defined by the number of rows, the number of columns, and the type of data.

The data type can be real, integer, Boolean, string, and polynomial. When two matrices have the same number of rows and columns, we say that they have the same shape.

In Scilab, vectors are a particular case of matrices, where the number of rows (or the number of columns) is equal to 1. Simple scalar variables do not exist in Scilab: a scalar variable is a matrix with 1 row and 1 column. Scilab was basically created to be able to perform matrices operations as fast as possible. With these high-level operators, most matrix algorithms do not require to use of loops. In fact, a Scilab script that performs the same operations with loops is typically from 10 to 100 times slower. This feature of Scilab is known as vectorization. In order to get a fast implementation of a given algorithm, the Scilab developer should always use high-level operations, so that each statement processes a matrix (or a vector) instead of a scalar.

Creating a Matrix of Real Values

 Suppose I want to create a 3 x 2 matrix of real numbers and let’s say our matrix is 

A = \begin{pmatrix}1 & 2 & 3 \\4 & 5 & 6 \\7 & 8 & 9\end{pmatrix}

For this, just write on the console window

--> A = [1, 2, 3; 4, 5, 6; 7, 8, 9]
A =

1. 2. 3.
4. 5. 6.
7. 8. 9.

The same matrix can also be obtained like this.

--> A = [ 1 2 3
> 4 5 6
> 7 8 9]
A =

1. 2. 3.
4. 5. 6.
7. 8. 9.

It’s a simpler way. Here, the blank space separates the columns while the new line separates the rows.

An empty matrix can be created by using empty square brackets.

--> A=[]
 A  =
    []

Scilab Commands for Some Special Kinds of Matrices

zeros(m,n)

It is for the matrix made of zeros.zeros(3,2) generate a 3 x 2 matrix having all the elements equal to zero.

eye(m,n)

It is for identity matrix.

eye(3,2) generates a 3 x 2 identity matrix. So, the command, eye(m,n) is used to create an identity matrix of m rows and n columns.

 linspace(m,n,p)

It is for linearly spaced vectors.

linspace(2, 10, 5) generates a row vector of 5 equally spaced values ranging exactly from 2 to 10.

--> linspace(2, 10, 5)
ans =

2. 4. 6. 8. 10.

linspace(2, 10) generates a row vector of 100 equally spaced values ranging exactly from 2 to 10. So, the default value of a number of equally spaced values is 100.

ones(m,n)

It is for a matrix made of ones.

ones(3,2) generates a 3 x 2 matrix having all the elements equal to 1.

grand

It’s a random number generator.

grand(2, 3, “exp”, 5) generates random numbers from the exponential distribution with a mean of 5.

--> grand(2, 3, "exp", 5)
ans =

3.1472369 0.4401028 4.6290769
7.350552 0.7660456 13.20044
rand(m,n)

It’s a random number generator. rand(2,3) looks like this.

--> rand(2,3)
ans =

0.2113249 0.0002211 0.6653811
0.7560439 0.3303271 0.6283918

Whenever you give the command rand(2,3), you will get same type of matrices but with different elements.

Row Matrix

 A row matrix can be created by using the colon “:” operator. The syntax for a row matrix is A = i:j. Suppose we write A = 5:9 then the output is 

--> A = 5:9
A  =

5.  6.   7.   8.   9.

Here the default step size is 1. If you want a specific step size then for that the syntax is A = i:s:j

--> A = 5:2:9
A  =

 5.  7.   9.

--> A = 5:2:8
A  =

5.  7.

Here you can notice that the last value is 7, which is smaller than 8.

How to get an element of a matrix?

The element of a matrix can be accessed directly with the A(i,j) syntax

Suppose we have a matrix A=testmatrix(‘hilb’,6). And we want to find the element in the second row and the fourth column then write A(2 ,4).

--> A=testmatrix('hilb',6)
A =

36. -630. 3360. -7560. 7560. -2772.
-630. 14700. -88200. 211680. -220500. 83160.
3360. -88200. 564480. -1411200. 1512000. -582120.
-7560. 211680. -1411200. 3628800. -3969000. 1552320.
7560. -220500. 1512000. -3969000. 4410000. -1746360.
-2772. 83160. -582120. 1552320. -1746360. 698544.

--> A(2 ,4)
ans  =

  211680.

You can also access the elements of a matrix in a given range. The syntax for it is A(i:j,k:l) Suppose we want a 2 x 3 matrix whose elements are taken from this matrix. Which are Ai;j for i = 1; 2 and j = 2; 4. In Scilab, it can be done in just one statement. Write the syntax B = A(1:2,2:4)

  • If you just write A then it will give you the whole matrix. Another way to obtain the whole matrix is A(:,:).
  • A(2:5,3) gives you the elements in rows from 2 to 5, in column 3.
  • A(2,3:5) gives you the elements in row 2, in columns from 3 to 5. If you write A(2,5:3) then you will get an empty matrix. So, to get a correct matrix the column number should be from lower to higher.
  • A(3,:) gives you row 3.
  • A(:,3) gives you column 3.
  • If you write A(3,$) then it will give you the element in row 3, at the last column
  • Similarly, A($,3) gives you the element at the last row and 3rd column
  • A($-1,$-1) gives you the element in the second last row and the second the last column.

Mathematical Operation in Scilab for Real Matrices

The rules for the “+” and “” operators are directly applied from the usual algebra.

--> A = [0 1 2
  > 3 4 5]

 A  =

 0.  1.   2.
3.  4.   5.

--> B = [6 7 8
  > 9 10 11]
B  =

6.  7.    8.
9.  10.   11.

--> A+B
ans  =

6.    8.    10.
12.   14.   16.

--> A-B
ans  =

-6.  -6.  -6.
-6.  -6.  -6.

If you add a scalar to a matrix then the addition is performed element by element.

--> A = [0 1
  > 2 3]
A  =

0. 1.
2. 3.

--> A+1
ans  =

1. 2.
3. 4.

*” operator is used for the multiplication of two matrices.

--> A = ones (3 ,3)
A  =

1. 1.   1.
1. 1.   1.
1. 1.   1.

--> B = 3 * ones (3 ,3)
B  =

3. 3.   3.
3. 3.   3.
3. 3.   3.

--> A*B
ans  =

9. 9.   9.
9. 9.   9.
9. 9.   9.

But if you write “.*” as the operator then the answer will be different. Let’s check it.

--> A.*B
ans  =

3. 3.   3.
3. 3.   3.
3. 3.   3.

Here “.*” is the operator for element-wise multiplication. Here each element of the first matrix is multiplied by the corresponding element of the second matrix.

The smallest and largest element of a matrix can be obtained by the command min(A) and max(A) respectively.

--> A = [3 2 4
> 6 0 5
  > 8 -1 2]
A  =

3. 2.   4.
6. 0.   5.
8. -1.   2.

--> min(A)
ans  =

  -1.

--> max(A)
ans  =

   8.

The product of all elements in the matrix can be obtained by the command prod(A).

--> prod(A)
ans  =

   0.

The sine value for the matrix A can be obtained by the operator

--> sin(A)
ans  =

  0.14112     0.9092974  -0.7568025
-0.2794155   0.         -0.9589243
   0.9893582  -0.841471    0.9092974

Similar operations can be obtained for other functions also as trigonometric, exponential, logarithmic, etc.

The determinant of the matrix A can be obtained by using the command   

--> det(A) 
ans  =

   47.

The rank of the matrix can be obtained by using the command

--> rank(A)
ans  =

   3.

A trace of the matrix can be obtained by using the command

--> trace(A)
ans  =

   5.

Eigenvalues of the matrix can be obtained by using the command

--> spec(A)
ans  =

  9.16183  + 0.i
-2.080915 + 0.8942998i
  -2.080915 - 0.8942998i

Leave a Comment

Your email address will not be published. Required fields are marked *

Shopping Cart
Right click not allowed
Index
Scroll to Top