Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
12 views

Lab-02 Matrix Operations

Uploaded by

Abdullah Ibrahim
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Lab-02 Matrix Operations

Uploaded by

Abdullah Ibrahim
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

UNIVERSITY OF ENGINEERING AND TECHNNOLOGY, TAXILA

DEPARTMENT OF ELECTRICAL ENGINEERING

SIGNALS & SYSTEMS LAB


Lab-02: Matrix Operations
OBJECTIVE:
To create matrices in MATLAB and understand different operations involving matrices, i.e.,
arithmetic operations, indexing, concatenation, generation through different built-in
functions, etc.

LEARNING TASKS:
1. Matrix Creation & Indexing
A matrix is an array of numbers. To type a matrix into MATLAB you must
• begin with a square bracket [
• separate elements in a row with spaces or commas ,
• use a semicolon ; to separate rows
• end the matrix with another square bracket ]
Here is a typical example to create a 3 × 3 matrix given below in MATLAB
1 2 3
𝑋 = [4 5 6]
7 8 9
>> X = [1 2 3; 4 5 6; 7 8 9]
TASK 1: Generate a matrix B and a matrix C of 𝟐 × 𝟒 and 𝟑 × 𝟏 sizes,
respectively. Set matrix elements of your choice. Write down the commands
and generated matrices.

Element(s) of a matrix can be accessed through indexing by specifying location(s). For


example, to access element located in the second row and first column of matrix X, the
command is
>> X(2,1)
ans =
4
Semester: Spring 2024
Session: 2K22 Designed by: Dr. Junaid Mir & Engr. Zainab Shahid
UNIVERSITY OF ENGINEERING AND TECHNNOLOGY, TAXILA
DEPARTMENT OF ELECTRICAL ENGINEERING

The colon operator : can also be used to pick out a certain row(s) or column(s). Always
remember that while indexing, row index is mentioned first followed by column index. For
example, the commands below return second row and all columns of matrix X. Colon operator
alone stands for all columns or all rows.
>> X = [1 2 3; 4 5 6; 7 8 9]
>> X(2,:)
ans =
4 5 6

Similarly, a sub-matrix Y is created containing all rows of X and second and third columns.

>> Y = X(:,2:3) or Y = X([1,2,3],[2,3])


Y =
2 3
5 6
8 9
Another example of indexing is given below where matrix B is created from matrix X through
indexing by assessing the last row and all columns. The used keyword end denotes the last
index in the specified dimension.

>> B = X(end,:) or B = X(3,[1,2,3])


B =
7 8 9

TASK 2: Create a matrix C and write down the commands to create matrix C
and the given sub-matrices from matrix C through indexing.
10 20 30 40 50 60
𝐶 = [−1 −2 −3 −4 −5 −6]
6 5 4 3 2 1
20 30
10 30 50
𝐶1 = [−3 −4 −5] 𝐶2 = [−2 −3] 𝐶3 = [ ]
6 4 2
5 4

Semester: Spring 2024


Session: 2K22 Designed by: Dr. Junaid Mir & Engr. Zainab Shahid
UNIVERSITY OF ENGINEERING AND TECHNNOLOGY, TAXILA
DEPARTMENT OF ELECTRICAL ENGINEERING

2. Matrix Generation & Operations through Built-in functions


MATLAB provides various functions for elementary matrices generations and operations.
Detailed below are few built-in functions. Use MATLAB doc command to understand the
usage and syntax of these commands.

TASK 3: Write down commands to generate 𝟒 × 𝟑 identity matrix, 𝟏 × 𝟖 zeros


matrix, 𝟑 × 𝟑 diagonal matrix with elements -3 -2 and -1, and 𝟓 × 𝟐 matrix with all
elements equal to 0.27. Also give the matrices.

TASK 4: Write down command to generate a rectangular matrix of order 𝟓 × 𝟑


having random values between 0 and 20. Also give the matrix.

Semester: Spring 2024


Session: 2K22 Designed by: Dr. Junaid Mir & Engr. Zainab Shahid
UNIVERSITY OF ENGINEERING AND TECHNNOLOGY, TAXILA
DEPARTMENT OF ELECTRICAL ENGINEERING

3. Matrix Manipulations
To create a vector version of any matrix X, simply write X(:). To interchange first and second
rows of X, use the vector of row indices together with the colon operator like below

>> X = [1 2 3; 4 5 6; 7 8 9]
>> C = X([2,1,3],:)
C =
4 5 6
1 2 3
7 8 9

A row or a column of a matrix or any particular element of a matrix can be set to any particular
value by simple assignment. To delete a particular row or a column, set it to a null vector [].

>> X(2,:) = []
X =
1 2 3
7 8 9
Matrices can be constructed in a block form through concatenation. For example, a matrix D
is defined through concatenation after initially defining a matrix C as

>> C = [1 2; 3 4]
C =
1 2
3 4

>> D = [C zeros(2); ones(1,2) eye(1,2)]


D =
1 2 0 0
3 4 0 0
1 1 1 0

TASK 5: Create a matrix D given above and then write down the commands to
1. Generate a matrix D1 by swapping the second and last column of matrix D
2. Generate a matrix D2 by omitting the last column of matrix D1
3. Setting the middle element of D2 to 100.42

Semester: Spring 2024


Session: 2K22 Designed by: Dr. Junaid Mir & Engr. Zainab Shahid
UNIVERSITY OF ENGINEERING AND TECHNNOLOGY, TAXILA
DEPARTMENT OF ELECTRICAL ENGINEERING

Matrix manipulation can also be performed with the help of comparison operators, i.e., greater
than >, greater than or equal to >=, less than < , less than or equal to <=, not operator ! ,
etc. These operators are useful for applying conditions on matrices. For example, if we want
to replace all elements greater then 5 in a matrix equal to 0, we can do so by following set of
commands
>> X = [1 2 3; 4 5 6; 7 8 9]
>> X(X>=5) = 0
X =
1 2 3
4 0 0
0 0 0
The command X(X>=5) = 0 sets all elements of matrix X greater than 5 to 0. Similarly, to
add 100 in all elements of matrix X greater than 5, the syntax of commands will be

>> X = [1 2 3; 4 5 6; 7 8 9]
>> X(X>5) = X(X>5) + 100
X =
1 2 3
4 5 106
107 108 109

TASK 6: Create a matrix X given below


10 20 −30
𝑋 = [ 40 −50 60 ]
−70 80 90
Write commands to perform the following tasks. Also give the output matrices.
For each task first generate matrix X else the operations will be overlaid.
1. Multiply the negative numbers of matrix X with -1
2. Replace all elements between -40 and +40 with zero
3. Subtract 100 from all positive numbers

Semester: Spring 2024


Session: 2K22 Designed by: Dr. Junaid Mir & Engr. Zainab Shahid
UNIVERSITY OF ENGINEERING AND TECHNNOLOGY, TAXILA
DEPARTMENT OF ELECTRICAL ENGINEERING

4. Matrix & Array Arithmetic Operations


MATLAB has two different types of arithmetic operations: matrix arithmetic operations and
array arithmetic operations. Given two matrices A and B following arithmetic operations can
be performed on matrices
A+B or B+A Matrix Addition; valid if A and B are of same size
Matrix Multiplication; valid if number of columns in A are equal to the
A*B
number of rows in B
A^2 or A*A Matrix Square; valid if A is a square matrix
α*A or A*α Matrix Scaling; multiply each element of A with scalar value α
Array arithmetic operations or array operations for short, are done element-by-element. The
period character . distinguishes the array operations from the matrix operations.

See the example below to understand the difference between matrix and array operation.

>> X = [1 1 1; 2 2 2; 3 3 3]
>> X.*X
ans =
1 1 1
4 4 4
9 9 9
>> X*X
ans =
6 6 6
12 12 12
18 18 18
TASK 7: Create a matrix A of order 𝟓 × 𝟑 and differentiate between A^2, A.^2, A*A.
Write down your findings/observations.

Semester: Spring 2024


Session: 2K22 Designed by: Dr. Junaid Mir & Engr. Zainab Shahid
UNIVERSITY OF ENGINEERING AND TECHNNOLOGY, TAXILA
DEPARTMENT OF ELECTRICAL ENGINEERING

5. Solving Linear Equations


One of the problems encountered most frequently in scientific computation is the solution of
systems of simultaneous linear equations. With matrix notation, a system of simultaneous
linear equations is written.
𝐴𝑥 = 𝑏
where there are as many equations as unknown. 𝐴 is a given square matrix of order 𝑛, 𝑏 is a
given column vector of 𝑛 components, and 𝑥 is an unknown column vector of 𝑛 components.
𝑏
In linear algebra we learn that the solution to 𝐴𝑥 = 𝑏 can be written as 𝑥 = = 𝐴−1 𝑏 ,where
𝐴
𝐴−1 is the inverse of matrix 𝐴.
For example, consider the following system of linear equations.
𝑥 + 2𝑦 + 3𝑧 = 1
4𝑥 + 5𝑦 + 6𝑧 = 1
7𝑥 + 8𝑦 = 1
where,
1 2 3 1
𝐴 = [4 5 6] 𝑏 = [ 1]
7 8 0 1
See the example below to solve these set of linear equations
>> A = [1 2 3; 4 5 6; 7 8 0]
>> b = [1; 1; 1]
>> x = inv(A)*b or x = A\b
x =
-1.0000
1.0000
-0.0000
TASK 8: Write commands to solve the following set of equations.
𝑤+x+y+z =4
2𝑤 − 𝑥 + 𝑧 = 2
3𝑤 + x − 𝑦 − 𝑧 = 2
𝑤 − 2x − 3𝑦 + 𝑧 = −3

Semester: Spring 2024


Session: 2K22 Designed by: Dr. Junaid Mir & Engr. Zainab Shahid

You might also like