Wednesday, April 18, 2012

Basic matrix commands and operations in Scilab

1.Matrix creation
To create a matrix in Scilab (or even Matlab) just follow the following syntax


Note: elements in one row can be written separated by commas (,) or spaces (blanks). So, it is more convenient  to use spaces

the previous code will return the following matrix


2. Row vector creation 
To create a row vector of elements starting from value (v1) and ending at value (v2) with a step (dv)


the previous code will return the following

Note: the step may be one of the following cases
  • Positive number. If a positive step is used, the end value should be greater than the start value, otherwise Scilab will return an empty matrix
  • Negative number. If a negative step is used, the end value should be less than the start value, otherwise Scilab will return an empty matrix
  • Zero. If step is zero then Scilab will return an empty matrix
Special case: you can create a row vector with step equals (1) directly without using the step value like the following


and this will return the following row vector


3. Resetting elements in matrix
You will use this in case where you want to change the value of certain element in the matrix to a new value. Consider the following code regarding the matrix we have created in section 1



this command will change the value of the element at (row 2, column 2) to the new value (100).


4. Retrieving (getting) matrix elements

4.1. Get element at (row, column). See the command below (it will return 8)

4.1. Get elements of certain column

and the previous code will return the following column



4.3. Get elements of certain row

 and this will return the following row


4.4. Get all elements of matrix as a column vector
This is useful when  someone needs to convert a two-dimensional matrix to a vector

it will return the following 

When Scilab converts a matrix to a vector (or as I call it "matrix unfolding") it reads each vector in the matrix and add it the new created vector.


The following topics will be covered later

5. Matrix filtering

6. Matrix concatenation

7. Matrix inverse

8. Matrix transpose

9. Matrix multiplication

10. Element-wise matrix operations 


No comments:

Post a Comment