Matrix Operations in Scilab is very easy before starting matrix operations let’s first discuss vectors.
You can easily perform add, subtraction, multiplication, calculation of eigenvalue and Eigenvectors, finding the inverse of the matrix, calculating linear equations and many more operations are easy with Scilab.
What is Vector in Scilab
We can create a vector in Scilab as below. Elements can be separated by, or space
1 2 3 | v=[1 2 3] or v=[1,2,3] v= 1. 2. 3. |
Finding length of vector
To find the length of vector length () method is used and the name of the vector is passed as an argument.
1 2 3 | length(v); ans= 3 |
Transpose of a vector
To find the transpose of a vector used ‘
with vector.
1 2 3 4 5 | -> v' ans = 1. 2. 3. |
Adding a number in vector
To add a scalar number in vector simple addition is done and addition is done with each element of vector.
1 2 3 | --> 5+v ans = 6. 7. 8. |
Subtracting a number in vector
Subtracting scalar number from each element following statement is used.
1 2 3 4 5 6 7 | --> v-1 ans = 0. 1. 2. w=[ 4 5 6] w = 4. 5. 6. |
Addition of two vectors
To add to vector store each one in a variable and perform add operation as below.
1 2 3 | v+w ans = 5. 7. 9. |
Subtraction of two vectors
It is also similar to variable subtraction
1 2 3 | w-v ans = 2. 3. 3. |
Matrix and matrices operations in scilab
Matrix is a rectangular arrangement of elements.
Creating matrix in scilab
1 2 3 4 | a=[1 2 3; 4 5 6] a = 1. 2. 3. 4. 5. 6. |
Accessing elements in Matrix Scilab
Scilab uses 1 based indexing to access elements.
1 2 3 4 5 6 | --> a(1,1) ans = 1. --> a(1,2) ans = 2. |
To access all elements of a row : is used
1 2 3 | --> a(1,:) ans = 1. 2. 3. |
a(:,: ) has the same meaning as a
here first :
represents column and second :
represents row,
To find second and third row of each column a (:,2:3)
1 2 3 4 5 | --> a (:,2:3) ans = 2. 3. 5. 6. |
Accessing the last element of matrix
a($) this will last element of matrix
1 2 3 4 | --> a($) ans = 6. |
Finding last element of first column
1 2 3 4 | --> a(1,$) ans = 3. |
Finding last element of each column
1 2 3 4 5 | --> a(:,$) ans = 3. 6. |
Adding a new row to matrix
Lets add a new row in matrix.
1 2 3 4 5 6 7 8 9 10 11 | --> d=[2 3 4; 5 6 7] d = 2. 3. 4. 5. 6. 7. --> d=[d;[ 8 9 10]] d = 2. 3. 4. 5. 6. 7. 8. 9. 10. |
Finding size of matrix in Scilab
To find the size of matrix the size() is used.
with size to gets row and columns
it can be used like
[row,column]=size(a)
here row and columns are stored in row and column variable
1 2 3 4 5 6 7 8 | --> size(a) ans = 2. 3. --> [row,column]=size(a) row = 2. column = 3. |
Finding square and cube of matrix
To find square and qube of matrix we have to use power operator
To find the power a square matrix is needed.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | --> b=[3 5 ;5 6] b = 3. 5. 5. 6. --> b^2 ans = 34. 45. 45. 61. --> b^3 ans = 327. 440. 440. 591. |
Addition and subtraction of matrix
Addition and subtraction of matrix is similar to add or subtract variables.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | --> a=[2 3; 4 5] a = 2. 3. 4. 5. --> b=[1 2 ;3 4] b = 1. 2. 3. 4. --> c=a+b; --> c=a+b c = 3. 5. 7. 9. |
Matrix multiplication in scilab
For multiplication of the matrix number of rows of the first matrix should match the number of columns of the second matrix.
1 2 3 4 5 | --> d=a*b d = 11. 16. 19. 28. |
Calculating the determinant of matrix
Let’s consider a matrix to find its determinant.
1 2 3 4 5 | c= [6 1 1;4 -2 5; 2 8 7] c = 6. 1. 1. 4. -2. 5. 2. 8. 7. |
To find the determinant use det()
1 2 3 | --> det(c) ans = -306. |
Inverse of matrix
To find the inverse of matrix inv() is used.
1 2 3 4 5 6 | inv(c) ans = 0.1764706 -0.003268 -0.0228758 0.0588235 -0.130719 0.0849673 -0.1176471 0.1503268 0.0522876 |
Eigenvalues of square matrix
To find the eigenvalue of matrix spec() is used.
1 2 3 4 5 6 | --> spec(c) ans = 11.248623 + 0.i 5.0928505 + 0.i -5.341474 + 0.i |
Functions related to matrix creation
To create different matrix following functions are used.
Creating zero matrix of 4×4
1 2 3 4 5 6 7 | --> zeros(4,4) ans = 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. |
Creating ones matrix of 4×4
1 2 3 4 5 6 7 | --> ones(4,3) ans = 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. |
Creating identity matrix if 3×3
1 2 3 4 5 6 | --> eye(3,3) ans = 1. 0. 0. 0. 1. 0. 0. 0. 1. |
Creating random matrix of 3×4
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | --> w=rand(3,4) w = column 1 to 3 0.2113249 0.3303271 0.8497452 0.7560439 0.6653811 0.685731 0.0002211 0.6283918 0.8782165 column 4 0.068374 0.5608486 0.6623569 |
Solving linear equation using matrices
Lets see how to solve linear equation in scilab.
- Get the equation.
- Represent data in matrix form
- Calculate inv(a)*b
- Result will be solution for linear equation
1 2 3 4 5 6 7 8 9 10 11 12 | X1+2x2-x3=5 2x1-3x2+5x3=4 3x1+4x2+2x3=3 Here A=[ 1 2 -1;2 -3 5; 3 4 2] B=[5;4;3] X=inv(a)*b; or a\b; x = inv 6.7142857 -2.5714286 -3.4285714 |
To create an identity matrix we use function eye(m,n). To create 3 by 3 identity matrix use eye(3,3).
Read More
Reference