Basics of Matlab (11CS)

Post on 06-May-2015

281 views 7 download

description

Basics of Matlab

Transcript of Basics of Matlab (11CS)

Basics of MatlabBy Ali Asghar Manjotho

Lecturer department of computer systems engineering, MUET, Jamshoro.

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]

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]

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.

Vector & Transpose•

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

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

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]

Matrix and Vector Indices

• Element at 2nd row, 5th column

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

Matrix and Vector Indices

• Element at 5th row, 2nd column

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

ans = 32

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

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

Matrix and Vector Indices

• Elements at column 4

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

1419242934

Matrix and Vector Indices

• Elements at row 3&4 column 2&3

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

ans = 22 2327 28

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

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]))

Matrix and Vector Indices

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

Matrix and Vector Indices

• >> I(5) ans = 7

Matrix and Vector Indices

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

Replacing matrix elements

• >> H(2,4) = 60

Replacing matrix elements

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

Deleting matrix elements

• >> I(5) = []

Deleting matrix elements

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

Deleting matrix elements

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

Deleting matrix elements

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

Zeros function• >> zeros(5,5)

Ones function• >> ones(5,5)

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

Rand function• >> rand(5,5)

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

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

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

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

Magic function• >> magic(3)

Diagonal of matrix

• >> diag(H)

Inverse of matrix

• >> inv(H)

Determinant of matrix

• >> det(H)

Number of elements

• >> numel(H)

Size of matrix

• >> size(H)

Max function

• >> max(H)

• >> max(max(H))

Min function

• >> min(H)

• >> min(min(H))

Sum function

• >> sum(H)

• >> sum(sum(H))

Mean function

• >> mean(H)

• >> mean(mean(H))

Matrix & Array operations

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

Matrix & Array operations

• >> J - K

Matrix & Array operations

• >> J * K

Matrix & Array operations

• >> J .* K

Plotting Functions

Plotting Functions

Plotting Functions

Plotting Functions

Plotting Functions

Plotting Functions