Basics of Matlab (11CS)

51
Basics of Matlab By Ali Asghar Manjotho Lecturer department of computer systems engineering, MUET, Jamshoro.

description

Basics of Matlab

Transcript of Basics of Matlab (11CS)

Page 1: Basics of Matlab (11CS)

Basics of MatlabBy Ali Asghar Manjotho

Lecturer department of computer systems engineering, MUET, Jamshoro.

Page 2: Basics of Matlab (11CS)

Matrix• Collection of rows and columns.• Represented by capital alphabets e.g. A, B, X.• Matrix elements are represented by small

alphabets e.g. x, y, z.• A =

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

Page 3: Basics of Matlab (11CS)

Vector• Vector is a matrix having either single row or

single column.• Single row matrix is called row vector.• Single column matrix is called column vector.•

• >> B = [4 2 8]• >> C = [7; 3; 9]

Page 4: Basics of Matlab (11CS)

Vector & linspace

• >> D = [5 10 15 20 25 30 35 40 45 50]• >> D = 5:5:50• >> D = linspace(5,50,10)

• >> E = [1:1:5 100:2:108]• >> E = [linspace(1,5,5) linspace(100,108,5)]

• linspace(X1,X2,N) generates linearly spaced vector, where X1 = start, X2 = end and N = number of points.

Page 5: Basics of Matlab (11CS)

Vector & Transpose•

• >> F = [5 10 15]• >> F’

• >> G = [2:2:10]’• >> G = [linspace(2,10,5)]’

Page 6: Basics of Matlab (11CS)

Matrix and Vector Indices

• >> H = [11 12 13 14 15; 16 17 18 19 20; 21 22

23 24 25; 26 27 28 29 30; 31 32 33 34 35]

Page 7: Basics of Matlab (11CS)

Matrix and Vector Indices

• Element at 2nd row, 5th column

• >> H(2,5) ans = 20

Page 8: Basics of Matlab (11CS)

Matrix and Vector Indices

• Element at 5th row, 2nd column

• >> H(5,2) (element at 5th row, 2nd column)

ans = 32

Page 9: Basics of Matlab (11CS)

Matrix and Vector Indices

• Elements at row 3

• >> H(3,1:5) ans = 21 22 23 24 25

• >> H(3,:) ans = 21 22 23 24 25

Page 10: Basics of Matlab (11CS)

Matrix and Vector Indices

• Elements at row 5

• >> H(5,1:5) ans = 31 32 33 34 35

• >> H(5,:) ans = 31 32 33 34 35

Page 11: Basics of Matlab (11CS)

Matrix and Vector Indices

• Elements at column 4

• >> H(1:5,4)• >> H(:,4) ans =

1419242934

Page 12: Basics of Matlab (11CS)

Matrix and Vector Indices

• Elements at row 3&4 column 2&3

• >> H(3:4,2:3)

ans = 22 2327 28

Page 13: Basics of Matlab (11CS)

Matrix and Vector Indices

• Elements at row 2 to 4 column 3 to 5

• >> H(2:4,3:5)

ans = 18 19 2023 24 2628 29 30

Page 14: Basics of Matlab (11CS)

Matrix and Vector Indices

• >> H(logical([0 1 0 1 0; 0 0 0 0 0; 0 0 0 1 0; 1 0 0 0 0; 0 0 1 0 1]))

Page 15: Basics of Matlab (11CS)

Matrix and Vector Indices

• >> I = [4 2 5 6 7 3 6]

Page 16: Basics of Matlab (11CS)

Matrix and Vector Indices

• >> I(5) ans = 7

Page 17: Basics of Matlab (11CS)

Matrix and Vector Indices

• >> I(2:5) ans = 2 5 6 7

Page 18: Basics of Matlab (11CS)

Replacing matrix elements

• >> H(2,4) = 60

Page 19: Basics of Matlab (11CS)

Replacing matrix elements

• >> H(1:2,2:3) = [20 30; 40 50]

Page 20: Basics of Matlab (11CS)

Deleting matrix elements

• >> I(5) = []

Page 21: Basics of Matlab (11CS)

Deleting matrix elements

• >> I(4:6) = []

Page 22: Basics of Matlab (11CS)

Deleting matrix elements

• >> H(1,:) = []

Page 23: Basics of Matlab (11CS)

Deleting matrix elements

• >> H(:,4:5) = []

Page 24: Basics of Matlab (11CS)

Zeros function• >> zeros(5,5)

Page 25: Basics of Matlab (11CS)

Ones function• >> ones(5,5)

Page 26: Basics of Matlab (11CS)

Ones function• >> 7*ones(5,5)

Page 27: Basics of Matlab (11CS)

Rand function• >> rand(5,5)

Page 28: Basics of Matlab (11CS)

Rand function• >> 100*rand(5,5)

Page 29: Basics of Matlab (11CS)

Rand function• >> fix(100*rand(5,5))

Page 30: Basics of Matlab (11CS)

Rand function• >> 1 + fix(100*rand(5,5))

Page 31: Basics of Matlab (11CS)

Randint function• >> randint(5,5,[50 100])

Page 32: Basics of Matlab (11CS)

Magic function• >> magic(3)

Page 33: Basics of Matlab (11CS)

Diagonal of matrix

• >> diag(H)

Page 34: Basics of Matlab (11CS)

Inverse of matrix

• >> inv(H)

Page 35: Basics of Matlab (11CS)

Determinant of matrix

• >> det(H)

Page 36: Basics of Matlab (11CS)

Number of elements

• >> numel(H)

Page 37: Basics of Matlab (11CS)

Size of matrix

• >> size(H)

Page 38: Basics of Matlab (11CS)

Max function

• >> max(H)

• >> max(max(H))

Page 39: Basics of Matlab (11CS)

Min function

• >> min(H)

• >> min(min(H))

Page 40: Basics of Matlab (11CS)

Sum function

• >> sum(H)

• >> sum(sum(H))

Page 41: Basics of Matlab (11CS)

Mean function

• >> mean(H)

• >> mean(mean(H))

Page 42: Basics of Matlab (11CS)

Matrix & Array operations

• >> J = [5 8; 4 1]• >> K = [1 2; 2 1]• >> J + K

Page 43: Basics of Matlab (11CS)

Matrix & Array operations

• >> J - K

Page 44: Basics of Matlab (11CS)

Matrix & Array operations

• >> J * K

Page 45: Basics of Matlab (11CS)

Matrix & Array operations

• >> J .* K

Page 46: Basics of Matlab (11CS)

Plotting Functions

Page 47: Basics of Matlab (11CS)

Plotting Functions

Page 48: Basics of Matlab (11CS)

Plotting Functions

Page 49: Basics of Matlab (11CS)

Plotting Functions

Page 50: Basics of Matlab (11CS)

Plotting Functions

Page 51: Basics of Matlab (11CS)

Plotting Functions