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 matrix operations as fast as possible.

With these high-level operators, most matrix algorithms do not require the 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 × 3 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) generates a 3 × 2 matrix having all the elements equal to zero.

eye(m,n)

It is for the identity matrix. eye(3,2) generates a 3 × 2 identity matrix.

linspace(m,n,p)

It is for linearly spaced vectors.


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

ones(m,n)

It is for a matrix made of ones. ones(3,2) generates a 3 × 2 matrix having all the elements equal to 1.

grand

It’s a random number generator.


--> 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. Whenever you give the command rand(2,3), you will get matrices of the same size but with different elements.


--> rand(2,3)
ans =

 0.2113249   0.0002211   0.6653811
 0.7560439   0.3303271   0.6283918
Row Matrix

A row matrix can be created by using the colon “:” operator. The syntax is A = i:j.


--> A = 5:9
A =

 5.  6.  7.  8.  9.

If you want a specific step size, 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.


--> A = testmatrix('hilb',6)
--> A(2,4)
ans =

 211680.

You can also access elements of a matrix in a given range using A(i:j,k:l).

Mathematical Operation in Scilab for Real Matrices


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

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

--> A + 1
ans =

 1.  2.
 3.  4.

--> A * B
ans =

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

--> A .* B
ans =

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

Here “.*” is the operator for element-wise multiplication.


--> min(A)
ans =

 -1.

--> max(A)
ans =

 8.

--> prod(A)
ans =

 0.

--> sin(A)
ans =

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

--> det(A)
ans =

 47.

--> rank(A)
ans =

 3.

--> trace(A)
ans =

 5.

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

Scroll to Top