{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

matlab - Very Basic MATLAB Peter J Olver January 2009...

This preview shows pages 1–7. Sign up to view the full content.

Very Basic MATLAB Peter J. Olver January, 2009 Matrices: Type your matrix as follows: Use space or , to separate entries, and ; or return after each row. >> A = [4 5 6 -9;5 0 -3 6;7 8 5 0; -1 4 5 1] or >> A = [4,5,6,-9;5,0,-3,6;7,8,5,0;-1,4,5,1] or >> A = [ 4 5 6 -9 5 0 -3 6 7 8 5 0 -1 4 5 1 ] The output will be: A = 4 5 6 -9 5 0 -3 6 7 8 5 0 -1 4 5 1 You can identify an entry of a matrix by >> A(2,3) ans = -3 A colon : indicates all entries in a row or column >> A(2,:) ans = 5 0 -3 6 >> A(:,3) ans = 6 -3 5 5 You can use these to modify entries >> A(2,3) = 10 A = 4 5 6 -9 5 0 10 6 7 8 5 0 -1 4 5 1 1

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
or to add in rows or columns >> A(5,:) = [0 1 0 -1] A = 4 5 6 -9 5 0 10 6 7 8 5 0 -1 4 5 1 0 1 0 -1 or to delete them >> A(:,2) = [ ] A = 4 6 -9 5 10 6 7 5 0 -1 5 1 0 0 -1 Accessing Part of a Matrix: >> A = [4,5,6,-9;5,0,-3,6;7,8,5,0;-1,4,5,1] A = 4 5 6 -9 5 0 -3 6 7 8 5 0 -1 4 5 1 >> A([1 3],:) ans = 4 5 6 -9 7 8 5 0 >> A(:,2:4) ans = 5 6 -9 0 -3 6 8 5 0 4 5 1 >> A(2:3,1:3) ans = 5 0 -3 7 8 5 2
Switching two rows in a matrix: >> A([3 1],:) = A([1 3],:) A = 7 8 5 0 5 0 -3 6 4 5 6 -9 -1 4 5 1 The Zero matrix: >> zeros(2,3) ans = 0 0 0 0 0 0 >> zeros(3) ans = 0 0 0 0 0 0 0 0 0 Identity Matrix: >> eye(3) ans = 1 0 0 0 1 0 0 0 1 Matrix of Ones: >> ones(2,3) ans = 1 1 1 1 1 1 Random Matrix: >> A = rand(2,3) A = 0.9501 0.4860 0.4565 0.2311 0.8913 0.0185 Note that the random entries all lie between 0 and 1. 3

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Transpose of a Matrix: >> A = [4,5,6,-9;5,0,-3,6;7,8,5,0;-1,4,5,1] A = 4 5 6 -9 5 0 -3 6 7 8 5 0 -1 4 5 1 >> transpose(A) ans = 4 5 7 -1 5 0 8 4 6 -3 5 5 -9 6 0 1 >> A’ ans = 4 5 7 -1 5 0 8 4 6 -3 5 5 -9 6 0 1 Diagonal of a Matrix: >> diag(A) ans = 4 0 5 1 Row vector: >> v = [1 2 3 4 5] v = 1 2 3 4 5 Column vector: >> v = [1;2;3;4;5] v = 1 2 3 4 5 4
or use transpose operation >> v = [1 2 3 4 5]’ v = 1 2 3 4 5 Forming Other Vectors: >> v = [1:5] v = 1 2 3 4 5 >> v = [10:-2:0] v = 10 8 6 4 2 0 >> v = linspace(0,1,6) v = 0 0.2000 0.4000 0.6000 0.8000 1.0000 Important: to avoid output, particularly of large matrices, use a semicolon ; at the end of the line: >> v = linspace(0,1,100); gives a row vector whose entries are 100 equally spaced points from 0 to 1. Size of a Matrix: >> A = [4 5 6 -9 7;5 0 -3 6 -2;7 8 5 0 5 ; -1 4 5 1 -9 ] A = 4 5 6 -9 7 5 0 -3 6 -2 7 8 5 0 5 -1 4 5 1 -9 >> size(A) ans = 4 5 >> [m,n] = size(A) m = 4 n = 5 5

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Output Formats The command format is used to change output format. The default is
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 17

matlab - Very Basic MATLAB Peter J Olver January 2009...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online