You are on page 1of 6

ECE MATH LAB

Laboratory Exercise 3

Matrix
From: mccormick.northwestern.edu

Objective

Familiarize with matrix generation and manipulation

Material
MATLAB r2020a or any version

Procedure: write the scripts on matlab command window. The indented figures are the answers to the
scripts. Do not include the bold texts. Observe the results and perform the task after.

1. row vector
>> v = [1 4 7 10 13]

v=

1 4 7 10 13

2. The Column vectors

>> w = [1;4;7;10;13]

w=

10

13
3. transpose operation

>> w = v’

w=

10

13

4. to access the first three elements of v

>> v(1:3)
ans =
147

5. all elements from the third through the last elements

>> v(3:end)

ans =

7 10 13
6. To enter a matrix A

>> A = [1 2 3; 4 5 6; 7 8 9]

A=

123

456

789
7. view a particular element in a matrix by specifying its location

>> A(2,1)

ans =

8. Indexing: substitute A(3,3)=9 by A(3,3)=0

>> A(3,3) = 0
A=
123
456
780

9. sub-matrix
extract a submatrix B consisting of rows 2 and 3 and columns 1 and 2 of the matrix A

>> B = A([2 3],[1 2])


B=
4 5
7 8

10. To interchange rows 1 and 2 of A, use the vector of row indices together with the colon
operator
>> C = A([2 1 3],:)
C=
456
123
7 80

11. To create a vector version of matrix A


>> A(:)
ans =
1
2
3
4
5
6
7
8
0
Other examples:
>> A
A=
123
456
789
>> A(2:3,2:3)
ans =
56
89
>> A(end:-1:1,end)
ans =
9
6
3
>> A([1 3],[2 3])
ans =
23
89

12. save and exit matlab

Task
Perform the following tasks and record the answers. Provide the screenshots of the scripts used, results,
and workspace. You can use matlab for pc or matlab for android.
Use minimum command lines and number of scripts as possible.

Clear the editor, workspace and command window of matlab before performing the task.
Answer the following:
1. Create matrix ‘a’ as shown below:

a=

12345

23456

34567

45678

2. Create a column vector v, from the elements of the 4th row of the matrix a:
3. delete the fourth row of ‘a’ using [ ]
4. delete the fifth column of ‘a’ [ ]

Paste the screenshots of your answers on a ms word document with the answers to the questions. Save
and submit as pdf format. Use the format below
LABORATORY EXERCISE NO. 3
___________TITLE__________

Name : ___________________________________________
Section: _______________________________

Task:
1.

2.

You might also like