Lab-02 Matrix Operations
Lab-02 Matrix Operations
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.
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.
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
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
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
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
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.