As Matlab®, Scilab is very powerful at computations with variables such as vectors and matrices. It has a lot of built-in functions that allows the user to perform complex computations and manipulations on vectors (1-D matrices) and matrices.

### Manual definition of a matrix

In Scilab, a variable of type matrix is defined in the following way:

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

This matrix has 3 rows and 3 columns. First are entered the values for the first row “1 2 3”. In order to complete the definition of the first row “;” is used. In the same manner rows 2 and 3 are defined. In the same way a 2×4 matrix can be defined:

-->B=[1 2 3 4;5 6 7 8] B = 1. 2. 3. 4. 5. 6. 7. 8. -->

The matrix definition has to be consistent. If the first row has 4 elements, for exemple, the second row has to have the same length. If not Scilab will output a error message:

-->C=[1 2 3 4;5 6 7 8 9] !--error 6 Inconsistent row/column dimensions. -->

A vector is in fact a matrix but only with one row or column. We can define the vector either by inserting the elements of the row:

-->V1=[1 2 3 4] V1 = 1. 2. 3. 4. -->

or by inserting the elements of the column using the “;” terminator:

-->V2=[1;2;3;4] V2 = 1. 2. 3. 4. -->

Keep in mind that for Scilab there is no difference between a vector or a matrix. Only the dimension is different between them, all variable are of type “Double”:

Once you have defined a matrix if you want to change some of it’s values there are two options. First is to rewrite the value that needs to be changed by pointing what row and column within the matrix is to be changed:

-->A(2,3)=60 A = 1. 2. 3. 4. 5. 6. 60. 8. -->

In this example we changed the value of the element from matrix A positioned in row 2 and column 3. Previously this value was 6 and after that we redefined it to 60.

Another method is to use the variable editor from Scilab. In order to do this use the Scilab console with the following instruction:

-->editvar A -->

After we hit <Enter> a windows opens that allows us to change any value of the matrix that we desire:

Using the variable editor allows us to change as many values of the matrix as we want. Also it gives the possibility of adding new rows or columns to the matrices. In this example we added a third row to matrix “A” containing four elements of 9.

Another way for matrix definition is by specifying the number of columns and rows which have assigned a value. For example:

-->B(3,1:3)=8 B = 0. 0. 0. 0. 0. 0. 8. 8. 8. -->

Here we have defined matrix B with 3 rows and 3 columns and we assigned value 8 to the 3rd row and columns from 1 to 3. Another example is:

-->C(2:3,2:3)=5 C = 0. 0. 0. 0. 5. 5. 0. 5. 5. -->

This time to the matrix C we have assigned value 5 from row 2 to 3 and from column 2 to 3.

By practising these example you should be able to extract and edit Scilab vectors and matrices. There are also some build-in functions within Scilab for matrix editing. About these we’ll discuss in another article.

For any questions, observations and queries regarding the article, use the comment form below.

Don’t forget to Like, Share and Subscribe!

## SHASHIKANT

Good article