Scilab Matrices

Download as pdf or txt
Download as pdf or txt
You are on page 1of 25

SCILAB MATRICES

OBJECTIVE

This chapter aims to discuss the variables used by Scilab


software the various operations associated with it.
OUTCOMES

At the end of this lesson, the students should be able to:


• Create Scilab matrices and arrays.
• Use Scilab in performing matrix operations.
• Perform mathematical operations involving matrices.
• Use Scilab matrices to solve linear algebra problems.
INTRODUCTION

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

[ ] Square bracket Encloses the entries of the matrix


, Comma Separates the values in different columns
; Semicolon Separates the values in different rows

In the following example, we create a 2 x 3 matrix with real entries.

A = [1, -2, 3; 3, 5 , -8]

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

The function linspace(i, j) generates a row vector of n (default value is


100) linearly equally spaced points between numbers i and j. If i or j
are complex numbers then linspace(i, j) returns a row vector of n
complex numbers, the real (resp. Imaginary) parts of the n complexes
are linearly equally spaced between the real (resp. Imaginary) parts
of i and j. The function linspace(i, j, n) gives a vector with n columns.
v = linspace( 0, 8 , 6)
v =
0. 1.6 3.2 4.8 6.4 8
Matrices can be made using colon operator or linspace.

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

Function Syntax Type of Matrix Formed

eye(m,n) Identity matrix


ones(m,n) Matrix with ones as the only entries
zeros(m,n) Matrix with zeros as the only entries

rand(m,n) Matrix with random numbers


Matrix Indexing
The elements of matrices in Scilab can be pin – pointed or indexed by placing an index
number inside a parenthesis after the variable name. Index numbers may either be a
single number or an ordered pair.
The index numbers follow the format as below:
Columns (n)
1 2 3 4

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

When performing multiplication of matrices, the following rules apply:


1. Inner dimensions are equal.
2. The size of the product matrix is the outermost dimensions of the two matrices.
AxB= mxp ∗ pxn =mxn
The product of two matrices is undefined if the above
conditions are not satisfied.

A=[3 4 2;3 -6 8];


-->D = A * A’;
D =
29. 1.
1. 109.
-->D^2
ans =
842. 138.
138. 11882.

-->B=2+ones(3,3);
-->A*B
ans =

27. 27. 27.


15. 15. 15.
-->B*A
!--error 10
Inconsistent multiplication.
Note that the last operation is undefined because of non conforming dimensions of matrices B and A.
Element Wise Operations
The element wise operations are associated with a dot ( . ) written before an operator. The operation is
done element-by-element.

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy