Scilab Matrices
Scilab Matrices
Scilab Matrices
OBJECTIVE
Matrix is the fundamental form that Scilab uses to store and manipulate
data. It is defined as an array of scalars arranged in rows and columns.
The size or dimension of a matrix is denoted by m x n where m is the
number of rows and n is the number of columns. Matrix can either be a
row vector or a column vector.
Row vectors are matrices with sizes of 1 x n (one row with several columns)
like [-3 5 7 12]: whereas, the column vectors have sizes of m x 1 (one column
3
with several rows) like 8 . in this section, we introduce scilab matrices
−10
and show how to create, delete and use them for some basic linear algebra
operations. The following is the list of symbols used to define a matrix.
SYMBOL DESCRIPTION
A=
1. -2. 3.
3. 5. -8.
In a simpler syntax, the space key separates the columns while the new line
separates the rows, as in the following illustration.
A = [1 -2 3
--> 3 5 -8]
A=
1. -2. 3.
3. 5. -8.
Creating A vector or A matrix with constant spacing
Colon operator
The colon ( : ) operator generates a vector with constant spacing. The
simplest commands are the following:
v = i: j or v = i: n: j
where i is the starting element, j is the ending element and n is the
constant difference with a default value of 1 when not present as in
the first case.
v = 3 : 8
v =
3. 4. 5. 6. 7. 8.
-->v = 3 : 2 : 13
v =
3. 5. 7. 9. 11. 13.
-->v = 8 : -2 : 3
v =
8. 6. 4.
Linspace command
v = [3:2:13;2 3 -5 3 1 4;linspace(2,9,6)]
v =
3. 5. 7. 9. 11. 13.
2. 3. - 5. 3. 1. 4.
2. 3.4 4.8 6.2 7.6 9.
Some Matrix Generating Functions
Rows (m) 1 5 9 13
1 13 8 3 4
2 6 10 14
2 25 9 1 2
A = 3 7 11 15
3 16 11 -1 8
4 8 12 16
4 18 15 12 18
element index i
To access a single element in the matrix,
1. Place inside the parenthesis the index number of the element counting the
elements downward.
2. Place inside the parenthesis, the row number (m), followed by the column
number separated by a comma.
ADDRESS LOCATION
A(2) Refers to 2nd element of matrix A counting downward
A(3,4) Refers to the element in row 3 and column 4
A(:) Represents all the row or column elements of the vector v
A(1:3) Represents the 1st through 3rd elements of v
A(:, 2) Denotes all the elements in the 2nd column of A
A(:, 1:4) Denotes all the elements in the 1st through 4th column of A
A(3, :) Denotes all the elements in the 3rd row of A
A(2:4, :) Denotes all the elements in the 2nd through 4th row of A
A(2:3,1:3) Denotes all the elements in the 2nd through 3rd row that are also in
the 1st though the third column
Matrix Concatenation
Matrices can be augmented using square brackets, which is the array
concatenation operator. Within the brackets, a space/comma is used
to separate rows; while a semicolon is used to separate columns.
a = [1 2;3 4];
-->cat_a = [ a, 2*a; 3*a, 4*a; 5*a, 6*a]
cat_a =
1. 2. 2. 4.
3. 4. 6. 8.
3. 6. 4. 8.
9. 12. 12. 16.
5. 10. 6. 12.
15. 20. 18. 24.
The size of a matrix can be increased or decreased by changing the entries of the original matrix.
Consider the following examples.
// Create matrix A
--> A= [1 5; 3 2]
A =
1. 5.
3. 2.
-->// Insert the value 4 at the indices (3,1).
--> A(3,1)=4
A =
1. 5.
3. 2.
4. 0.
-->// Reduce the size by deleting the first column.
--> A(:,1)=[ ]
A =
5.
2.
0.
The matrix function restructures a source matrix into a new matrix with a different
size. The transformation is performed column by column, which results to a matrix
with nm elements.
B = [1 3 5;6 -9 0]
B =
1. 3. 5.
6. - 9. 0.
-->C = matrix(B,3,2)
C =
1. - 9.
6. 5.
3. 0.
-->C = matrix(B,1,6)
C =
1. 6. 3. - 9. 5. 0.
Basic Matrix Operations
Symbol Operation
‘ Transpose of a matrix
^ Power
+ Addition
– Subtraction
* Multiplication
/ Right division
The rules for the basic operators also hold for basic operations on matrices. Note that the
operations addition/subtraction and exponentiation/multiplication are defined only if the
matrices involved have the required sizes.
//Create two matrices for Matrix Addition,
-->// Scalar Multiplication, Scalar Addition
--> A = [2 3;-2 5]
A =
2. 3.
- 2. 5.
--> B=[4 2;-1 7]
B =
4. 2.
- 1. 7.
--> B=2+ones(3,3)
B =
3. 3. 3.
3. 3. 3.
3. 3. 3.
--> A = [A+2]
A =
4. 5.
0. 7.
--> A=[A*2]
A =
8. 10.
0. 14.
Note:
1. In performing matrix addition and matrix subtraction, the sizes of the matrix arguments must be
equal.
2. In performing multiplication or addition of a matrix by a scalar quantity, the operation may be
performed inside or outside the square bracket as :
A = [1 2 3; 4 5 6]
B = [A + 3] or B = A+3
C = [A*3] or C = 3*A
Matrix multiplication
-->B=2+ones(3,3);
-->A*B
ans =
A = 2*ones(2,3)
A =
2. 2. 2.
2. 2. 2.
-->B = [3 5 9;-2 4 1]
B =
3. 5. 9.
-2. 4. 1.
--> A.*B
ans =
6. 10. 18.
- 4. 8. 2.
Some Matrix Functions
Syntax Definition
inv Inverse of a Matrix
det Determinant of a matrix
trace The sum of diagonal entries
rank The number of non zero rows of a matrix
spec Eigenvalues of A
rref Reduced row echelon form
sum Sum of array elements
length Total number of elements of the given matrix
size Size of the given matrix
gsort Sorts the element according to input parameters
min Minimum value in a Matrix
max Maximum value in a Matrix
tril Extracts the lower triangular matrix
triu Extracts the upper triangular matrix
WE’RE NOW READY FOR OUR ACTIVITY