Lab No. 02: Objective

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

Lab No.

02

Vectors and Matrices Manipulation in Matlab


Tools: PC, Matlab

Objective: To learn about vectors, mathematical operation on vectors, matrices, mathematical operation
on matrices, solution set of matrices in Matlab.

Theory and Matlab Work

Vector:
Vector is a sequence of regular or irregular numbers.

Defining a Row Vector in Matlab


>> A=[1 2 3 4 5]

A=

1 2 3 4 5

Defining a column vector in Matlab


>> A=[1;2;3;4;5]

A=

Defining a Vector of Regular interval in Matlab


>> A=[1:10]

A=

1 2 3 4 5 6 7 8 9 10

Defining Vector of Irregular Interval


>> B=[1:4:20]

B=
1 5 9 13 17

Accessing Element of a vector by mentioning position


>> B(4)

ans =

13

Mathematical operation on Vectors


Addition of vectors:
A=

2 3 4 5

>> B=[3 5 6 7]

B=

3 5 6 7

>> A+B

ans =

5 8 10 12

Subtraction of vectors
>> A-B

ans =

-1 -2 -2 -2

Multiplication of vectors
For multiplication it is necessary that the number of rows of first vector must be equal to number of
columns of second vector

>> A*B'

ans =

80

Division of vectors
>> A/B

ans =
0.6723

Division of number based on position by position


>> A./B

ans =

0.6667 0.6000 0.6667 0.7143

Matrices in Matlab
Matrix is combination of rows and column vectors.

Defining Matrix in Matlab


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

A=

1 2 3

4 5 6

7 8 9

Accessing an element of a Matrix


>> A(2,3)

ans =

Accessing a row of a Matrix


>> A(3,:)

ans =

7 8 9

Accessing a column of a Matrix


>> A(:,2)

ans =

8
Mathematical operation on Matrices
Addition of Matrices
For addition both the two matrices must be of same dimension.

A=

2 3 4

5 6 7

2 3 4

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

B=

1 2 3

4 5 6

7 8 9

>> A+B

ans =

3 5 7

9 11 13

9 11 13

Subtraction of Matrices
>> A-B

ans =

-2 -2 -2

3 1 1

0 0 0

Multiplication of Matrices
For multiplication of matrices the number of rows of first matrix must be equal to number of columns of
second matrix.

>> A*B

ans =
26 36 42

59 84 99

92 132 156

Division of Matrices
>> A/B

ans =

1.5000 -0.0000 -0.5000

0.7500 -0.0000 0.2500

0 0 1.0000

Transpose of a Matrix
>> A'

ans =

1 4 7

2 5 8

3 6 9

Determinant of a Matrix
>> det(B)

ans =

-8

Inverse Matrix of a Matrix


>> inv(B)

ans =

0.5000 -0.5000 0.0000

-3.2500 1.0000 1.2500

2.5000 -0.5000 -1.0000

Solution set of a Matrix


>> inv(B)/det(B)

ans =
-0.0625 0.0625 -0.0000

0.4062 -0.1250 -0.1562

-0.3125 0.0625 0.1250

Generating Identity Matrix in Matlab


>> eye(4,4)

ans =

1 0 0 0

0 1 0 0

0 0 1 0

0 0 0 1

Generating Matrix of Ones in Matlab


>> ones(3,3)

ans =

1 1 1

1 1 1

1 1 1

Generating Matrix of Zeros in Matlab


>> zeros(3,3)

ans =

0 0 0

0 0 0

0 0 0

Lab. Assignment
Perform the following Mathematical operation in Matlab on the given Matrix.

Z=

3 4 7

11 15 19

2 4 6
1) Natural Log Operation
2) Square root
3) General log
4) Power operation.

You might also like