MATLAB Programming Matrix Manipulation Plotting 2008 Applied Mathematics
MATLAB Programming • Matrix Manipulation • Plotting 數值方法 2008, Applied Mathematics NDHU 1
Matrix creation l Matrix generation l l l l eye ones zeros diag rand repmat reshape l Vector generation l l linspace equi-spacing 數值方法 2008, Applied Mathematics NDHU 2
Exercise l matrix creation 數值方法 2008, Applied Mathematics NDHU 3
rand l rand(m, n) l Create a matrix composed of m rows and n columns. All of its elements are uniformly sampled from [0 1] 數值方法 2008, Applied Mathematics NDHU 4
Matrix size l A=rand(m, n) l size(A) l Return row and column numbers of matrix A 數值方法 2008, Applied Mathematics NDHU 5
eye(n), ones(n), zeros(n) l eye(n) l Create an nxn identical matrix l ones(n) l Create an nxn matrix with all elements identical to one l zeros(n) l Create an nxn zero matrix 數值方法 2008, Applied Mathematics NDHU 6
Diagonal matrix l v=[1 2 3 4 5] l diag(v) l Create an diagonal matrix whose diagonal vector is identical to v 數值方法 2008, Applied Mathematics NDHU 7
Matrix multiplication l Validity l A*B is valid if the column number of A is identical to the row number of B l ones(5, 1)*[1 2 3 4 5] Form a 5 x 5 matrix. l Each of its five rows equals [1 2 3 4 5] l 數值方法 2008, Applied Mathematics NDHU 8
Matrix replication l v=[1 2 3 4 5] l repmat(v, n, m) Repeat v n times vertically l Repeat the result m times horizontally l l repmat(v, 5, 1) 數值方法 2008, Applied Mathematics NDHU 9
Reshape A=rand(m, n) B=reshape(A, p, q) Validity: mxn needs identical to pxq l B is a pxq matrix l Column major reshaping of a matrix l 數值方法 2008, Applied Mathematics NDHU 10
Column major A=[1 2 3; 4 5 6]; B=reshape(A, 3, 2) 1 3 4 5 2 6 數值方法 2008, Applied Mathematics NDHU 11
Column major B=reshape(A, 3, 2) 數值方法 2008, Applied Mathematics NDHU 12
Column major l A=[1 2 3; 4 5 6; 7 8 9; 10 11 12] l B=reshape(A, 3, 4) 數值方法 2008, Applied Mathematics NDHU 13
Vector creation l Direct l input a=[1 2 3 4 5 6 7 8 9 10] l Spacing a=1: 10 l a=1: 2: 100 l A=100: -5: 1 l 數值方法 2008, Applied Mathematics NDHU 14
Linspace l v=linspace(a, b, n) v is a vector which consists of n elements l These elements equally partition the interval [a b] l 數值方法 2008, Applied Mathematics NDHU 15
Plot l Plot points plot(x, y, ’. ’) l x and y have same length, such as n l This instruction draws n points, denoted by {(xi yi)}i l xi denotes the ith element of vector x l yi denotes the ith element of vector x 數值方法 2008, Applied Mathematics NDHU 16
Plot points and lines l plot(x, y) l n points and lines connecting two consecutive points 數值方法 2008, Applied Mathematics NDHU 17
Plot a function x=linspace(-5, 5, 100); y=cos(x); plot(x, y) l The output contains a set of points specified by vectors x and y l Two consecutive points are connected 數值方法 2008, Applied Mathematics NDHU 18
subplot x=linspace(-5, 5, 100); subplot(2, 1, 1) plot(x, cos(x)) subplot(2, 1, 2) plot(x, sin(x)) 數值方法 2008, Applied Mathematics NDHU 19
- Slides: 19