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