Download - Basics of Matlab (11CS)
![Page 1: Basics of Matlab (11CS)](https://reader035.vdocuments.net/reader035/viewer/2022062303/554a0c64b4c9055c598b46b0/html5/thumbnails/1.jpg)
Basics of MatlabBy Ali Asghar Manjotho
Lecturer department of computer systems engineering, MUET, Jamshoro.
![Page 2: Basics of Matlab (11CS)](https://reader035.vdocuments.net/reader035/viewer/2022062303/554a0c64b4c9055c598b46b0/html5/thumbnails/2.jpg)
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)](https://reader035.vdocuments.net/reader035/viewer/2022062303/554a0c64b4c9055c598b46b0/html5/thumbnails/3.jpg)
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)](https://reader035.vdocuments.net/reader035/viewer/2022062303/554a0c64b4c9055c598b46b0/html5/thumbnails/4.jpg)
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)](https://reader035.vdocuments.net/reader035/viewer/2022062303/554a0c64b4c9055c598b46b0/html5/thumbnails/5.jpg)
Vector & Transpose•
• >> F = [5 10 15]• >> F’
• >> G = [2:2:10]’• >> G = [linspace(2,10,5)]’
![Page 6: Basics of Matlab (11CS)](https://reader035.vdocuments.net/reader035/viewer/2022062303/554a0c64b4c9055c598b46b0/html5/thumbnails/6.jpg)
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)](https://reader035.vdocuments.net/reader035/viewer/2022062303/554a0c64b4c9055c598b46b0/html5/thumbnails/7.jpg)
Matrix and Vector Indices
• Element at 2nd row, 5th column
• >> H(2,5) ans = 20
![Page 8: Basics of Matlab (11CS)](https://reader035.vdocuments.net/reader035/viewer/2022062303/554a0c64b4c9055c598b46b0/html5/thumbnails/8.jpg)
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)](https://reader035.vdocuments.net/reader035/viewer/2022062303/554a0c64b4c9055c598b46b0/html5/thumbnails/9.jpg)
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)](https://reader035.vdocuments.net/reader035/viewer/2022062303/554a0c64b4c9055c598b46b0/html5/thumbnails/10.jpg)
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)](https://reader035.vdocuments.net/reader035/viewer/2022062303/554a0c64b4c9055c598b46b0/html5/thumbnails/11.jpg)
Matrix and Vector Indices
• Elements at column 4
• >> H(1:5,4)• >> H(:,4) ans =
1419242934
![Page 12: Basics of Matlab (11CS)](https://reader035.vdocuments.net/reader035/viewer/2022062303/554a0c64b4c9055c598b46b0/html5/thumbnails/12.jpg)
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)](https://reader035.vdocuments.net/reader035/viewer/2022062303/554a0c64b4c9055c598b46b0/html5/thumbnails/13.jpg)
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)](https://reader035.vdocuments.net/reader035/viewer/2022062303/554a0c64b4c9055c598b46b0/html5/thumbnails/14.jpg)
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)](https://reader035.vdocuments.net/reader035/viewer/2022062303/554a0c64b4c9055c598b46b0/html5/thumbnails/15.jpg)
Matrix and Vector Indices
• >> I = [4 2 5 6 7 3 6]
![Page 16: Basics of Matlab (11CS)](https://reader035.vdocuments.net/reader035/viewer/2022062303/554a0c64b4c9055c598b46b0/html5/thumbnails/16.jpg)
Matrix and Vector Indices
• >> I(5) ans = 7
![Page 17: Basics of Matlab (11CS)](https://reader035.vdocuments.net/reader035/viewer/2022062303/554a0c64b4c9055c598b46b0/html5/thumbnails/17.jpg)
Matrix and Vector Indices
• >> I(2:5) ans = 2 5 6 7
![Page 18: Basics of Matlab (11CS)](https://reader035.vdocuments.net/reader035/viewer/2022062303/554a0c64b4c9055c598b46b0/html5/thumbnails/18.jpg)
Replacing matrix elements
• >> H(2,4) = 60
![Page 19: Basics of Matlab (11CS)](https://reader035.vdocuments.net/reader035/viewer/2022062303/554a0c64b4c9055c598b46b0/html5/thumbnails/19.jpg)
Replacing matrix elements
• >> H(1:2,2:3) = [20 30; 40 50]
![Page 20: Basics of Matlab (11CS)](https://reader035.vdocuments.net/reader035/viewer/2022062303/554a0c64b4c9055c598b46b0/html5/thumbnails/20.jpg)
Deleting matrix elements
• >> I(5) = []
![Page 21: Basics of Matlab (11CS)](https://reader035.vdocuments.net/reader035/viewer/2022062303/554a0c64b4c9055c598b46b0/html5/thumbnails/21.jpg)
Deleting matrix elements
• >> I(4:6) = []
![Page 22: Basics of Matlab (11CS)](https://reader035.vdocuments.net/reader035/viewer/2022062303/554a0c64b4c9055c598b46b0/html5/thumbnails/22.jpg)
Deleting matrix elements
• >> H(1,:) = []
![Page 23: Basics of Matlab (11CS)](https://reader035.vdocuments.net/reader035/viewer/2022062303/554a0c64b4c9055c598b46b0/html5/thumbnails/23.jpg)
Deleting matrix elements
• >> H(:,4:5) = []
![Page 24: Basics of Matlab (11CS)](https://reader035.vdocuments.net/reader035/viewer/2022062303/554a0c64b4c9055c598b46b0/html5/thumbnails/24.jpg)
Zeros function• >> zeros(5,5)
![Page 25: Basics of Matlab (11CS)](https://reader035.vdocuments.net/reader035/viewer/2022062303/554a0c64b4c9055c598b46b0/html5/thumbnails/25.jpg)
Ones function• >> ones(5,5)
![Page 26: Basics of Matlab (11CS)](https://reader035.vdocuments.net/reader035/viewer/2022062303/554a0c64b4c9055c598b46b0/html5/thumbnails/26.jpg)
Ones function• >> 7*ones(5,5)
![Page 27: Basics of Matlab (11CS)](https://reader035.vdocuments.net/reader035/viewer/2022062303/554a0c64b4c9055c598b46b0/html5/thumbnails/27.jpg)
Rand function• >> rand(5,5)
![Page 28: Basics of Matlab (11CS)](https://reader035.vdocuments.net/reader035/viewer/2022062303/554a0c64b4c9055c598b46b0/html5/thumbnails/28.jpg)
Rand function• >> 100*rand(5,5)
![Page 29: Basics of Matlab (11CS)](https://reader035.vdocuments.net/reader035/viewer/2022062303/554a0c64b4c9055c598b46b0/html5/thumbnails/29.jpg)
Rand function• >> fix(100*rand(5,5))
![Page 30: Basics of Matlab (11CS)](https://reader035.vdocuments.net/reader035/viewer/2022062303/554a0c64b4c9055c598b46b0/html5/thumbnails/30.jpg)
Rand function• >> 1 + fix(100*rand(5,5))
![Page 31: Basics of Matlab (11CS)](https://reader035.vdocuments.net/reader035/viewer/2022062303/554a0c64b4c9055c598b46b0/html5/thumbnails/31.jpg)
Randint function• >> randint(5,5,[50 100])
![Page 32: Basics of Matlab (11CS)](https://reader035.vdocuments.net/reader035/viewer/2022062303/554a0c64b4c9055c598b46b0/html5/thumbnails/32.jpg)
Magic function• >> magic(3)
![Page 33: Basics of Matlab (11CS)](https://reader035.vdocuments.net/reader035/viewer/2022062303/554a0c64b4c9055c598b46b0/html5/thumbnails/33.jpg)
Diagonal of matrix
• >> diag(H)
![Page 34: Basics of Matlab (11CS)](https://reader035.vdocuments.net/reader035/viewer/2022062303/554a0c64b4c9055c598b46b0/html5/thumbnails/34.jpg)
Inverse of matrix
• >> inv(H)
![Page 35: Basics of Matlab (11CS)](https://reader035.vdocuments.net/reader035/viewer/2022062303/554a0c64b4c9055c598b46b0/html5/thumbnails/35.jpg)
Determinant of matrix
• >> det(H)
![Page 36: Basics of Matlab (11CS)](https://reader035.vdocuments.net/reader035/viewer/2022062303/554a0c64b4c9055c598b46b0/html5/thumbnails/36.jpg)
Number of elements
• >> numel(H)
![Page 37: Basics of Matlab (11CS)](https://reader035.vdocuments.net/reader035/viewer/2022062303/554a0c64b4c9055c598b46b0/html5/thumbnails/37.jpg)
Size of matrix
• >> size(H)
![Page 38: Basics of Matlab (11CS)](https://reader035.vdocuments.net/reader035/viewer/2022062303/554a0c64b4c9055c598b46b0/html5/thumbnails/38.jpg)
Max function
• >> max(H)
• >> max(max(H))
![Page 39: Basics of Matlab (11CS)](https://reader035.vdocuments.net/reader035/viewer/2022062303/554a0c64b4c9055c598b46b0/html5/thumbnails/39.jpg)
Min function
• >> min(H)
• >> min(min(H))
![Page 40: Basics of Matlab (11CS)](https://reader035.vdocuments.net/reader035/viewer/2022062303/554a0c64b4c9055c598b46b0/html5/thumbnails/40.jpg)
Sum function
• >> sum(H)
• >> sum(sum(H))
![Page 41: Basics of Matlab (11CS)](https://reader035.vdocuments.net/reader035/viewer/2022062303/554a0c64b4c9055c598b46b0/html5/thumbnails/41.jpg)
Mean function
• >> mean(H)
• >> mean(mean(H))
![Page 42: Basics of Matlab (11CS)](https://reader035.vdocuments.net/reader035/viewer/2022062303/554a0c64b4c9055c598b46b0/html5/thumbnails/42.jpg)
Matrix & Array operations
•
• >> J = [5 8; 4 1]• >> K = [1 2; 2 1]• >> J + K
![Page 43: Basics of Matlab (11CS)](https://reader035.vdocuments.net/reader035/viewer/2022062303/554a0c64b4c9055c598b46b0/html5/thumbnails/43.jpg)
Matrix & Array operations
•
• >> J - K
![Page 44: Basics of Matlab (11CS)](https://reader035.vdocuments.net/reader035/viewer/2022062303/554a0c64b4c9055c598b46b0/html5/thumbnails/44.jpg)
Matrix & Array operations
•
• >> J * K
![Page 45: Basics of Matlab (11CS)](https://reader035.vdocuments.net/reader035/viewer/2022062303/554a0c64b4c9055c598b46b0/html5/thumbnails/45.jpg)
Matrix & Array operations
•
• >> J .* K
![Page 46: Basics of Matlab (11CS)](https://reader035.vdocuments.net/reader035/viewer/2022062303/554a0c64b4c9055c598b46b0/html5/thumbnails/46.jpg)
Plotting Functions
![Page 47: Basics of Matlab (11CS)](https://reader035.vdocuments.net/reader035/viewer/2022062303/554a0c64b4c9055c598b46b0/html5/thumbnails/47.jpg)
Plotting Functions
![Page 48: Basics of Matlab (11CS)](https://reader035.vdocuments.net/reader035/viewer/2022062303/554a0c64b4c9055c598b46b0/html5/thumbnails/48.jpg)
Plotting Functions
![Page 49: Basics of Matlab (11CS)](https://reader035.vdocuments.net/reader035/viewer/2022062303/554a0c64b4c9055c598b46b0/html5/thumbnails/49.jpg)
Plotting Functions
![Page 50: Basics of Matlab (11CS)](https://reader035.vdocuments.net/reader035/viewer/2022062303/554a0c64b4c9055c598b46b0/html5/thumbnails/50.jpg)
Plotting Functions
![Page 51: Basics of Matlab (11CS)](https://reader035.vdocuments.net/reader035/viewer/2022062303/554a0c64b4c9055c598b46b0/html5/thumbnails/51.jpg)
Plotting Functions