Applied Numerical Methods Using MATLAB. Won Y. Yang
(Eq. (2.1.10)) for M> N (over‐determined case) and one of the many solutions that is not always the same as the minimum‐norm solution (Eq. (2.1.7)) for M< N (under‐determined case). But for the case where A is rectangular and has rank deficiency, what MATLAB gives us may be useless. Therefore, you must pay attention to the warning message about rank deficiency, which might tell you not to count on the dead‐end solution made by the backslash(\) operator. To find an alternative in the case of rank deficiency, you had better resort to the singular value decomposition (SVD) (see Problem 2.8 for details).
For the moment, let us continue to try more operations on matrices.
>A1./A2 % termwise right division ans= 1 1 1 -2 Inf 2 >A1.\A2 % termwise left division ans= 1 1 1 -0.5 0 0.5 >format rat, B̂-1 %represent the numbers (of B<sup>‐1</sup><![CDATA[) in fractional form ans= 0 -1/3 -1/2 –1/6 >inv(B) % inverse matrix, equivalently ans= 0 -1/3 -1/2 –1/6 >B.̂-1 % element-wise inversion(reciprocal of each element) ans= 1 -1/2 -1/3 Inf >B̂2 % square of B, i.e., B<sup>2</sup><![CDATA[=B*B ans= 7 -2 -3 6 >B.̂2 % element-wise square(square of each element) ans= 1(
There are more useful MATLAB commands worthwhile to learn by heart.
Remark 1.3 More Useful Commands for Vector/Matrix Operations
1 (1) We can use the commands ‘ zeros()’, ‘ ones()’, and eye()’ to construct a matrix of specified size or the same size as an existing matrix that has only zeros, only ones, or only ones/zeros on/off its diagonal.>Z=zeros(2,3) % or zeros(size(A1)) yielding a 2x3 zero matrix Z= 0 0 0 0 0 0 >E=ones(size(B)) % or ones(3,2) yielding a 3x2 one matrix E= 1 1 1 1 1 1 >I=eye(2) % yielding a 2x2 identity matrix I= 1 0 0 1
2 (2) We can use the ‘ diag()’ command to make a column vector composed of the diagonal elements of a matrix or to make a diagonal matrix with on‐diagonal elements taken from a vector given as the input argument.>A1=[-1 2 3; 4 5 2] A1= -1 2 3 4 5 2 >a=diag(A1) % The column vector consisting of diagonal elements a= -1 5 >diag(a) % The column vector consisting of diagonal elements ans= -1 0 0 5
3 (3) We can use the commands ‘ sum()’/‘ prod()’ to get the sum/product of elements in a vector or a matrix, column‐wisely first (along the first nonsingleton dimension).>sa1=sum(a1) % sum of all the elements in vector a1 sa1= 4 %∑a1(n) = - 1 + 2 + 3 = 4 >sA1=sum(A1) % sum of all the elements in each column of matrix A1 sA1= 3 7 5 % >SA1=sum(sum(A1)) % sum of all elements in matrix A1 SA1= 15 % >pa1=prod(a1) % product of all the elements in vector a1 pa1= -6 %∏a1(n) = (-1) × 2 × 3 = - 6 >pA1=prod(A1) % product of all the elements in each column of matrix A1 pA1= -4 10 6 % >PA1=prod(prod(A1))% product of all the elements of matrix A1 PA1= -240 %
4 (4) We can use the commands ‘ max()’/‘ min()’ to find the first maximum/minimum number and its index in a vector or in a matrix given as the input argument.>[aM,iM]=max(a2) aM= 5, iM= 2 %means that the max. element of vector a2 is a2(2)=5 >[AM,IM]=max(A1) AM= 4 5 3 IM= 2 2 1 %means that the max. elements of each column of A1 are A1(2,1)=4, A1(2,2)=5, A1(1,3)=3 >[AMx,J]=max(AM) AMx= 5, J= 2 %implies that the max. element of A1 is A1(IM(J),J)=A1(2,2)=5
5 (5) We can use the commands ‘ rot90()’/‘ fliplr()’/‘ flipud()’ to rotate a matrix by an integer multiple of 90° and to flip it left–right/up–down.>A1, A3=rot90(A1), A4=rot90(A1,-2) A1= -1 2 3 4 5 2 A3= 3 2 %90o rotation 2 5 -1 4 A4= 2 5 4 %90 ox(-2) rotation 3 2 -1 >A5=fliplr(A1) %flip left-right A5= 3 2 -1 2 5 4 >A6=flipud(A1) %flip up-down A6= 4 5 2 -1 2 3
6 (6) We can use the ‘ reshape()’ command to change the row–column size of a matrix with its elements preserved (column‐wisely first).>A7=reshape(A1,3,2) A7= -1 5 4 3 2 2 >A8=reshape(A1,6,1), A8=A1(:) %makes super-column vector A8= -1 4 2 5 3 2
7 (7) We can use the ‘ repmat(A,M,N)’ command to repeat a matrix A to make a large matrix consisting of an M× N tiling of copies of A.>A9=repmat(A1,2,3) A9= -1 2 3 -1 2 3 -1 2 3 4 5 2 4 5 2 4 5 2 -1 2 3 -1 2 3 -1 2 3 4 5 2 4 5 2 4 5 2
8 (8) We can use the ‘ find’ command to find the row/column indices of the entries satisfying the condition specified as its input argument.>[ir,ic]=find(2<A1&A1<5) ir = 2 ic = 1 1 3This means that the matrix A1 has two entries, A1(2,1) and A1(1,3), satisfying the two inequalities, ‘greater than 2’ and ‘smaller than 5’.
1.1.8 Random Number Generators
MATLAB has the built‐in functions, ‘ rand()
’/‘ randn()
’, to generate random numbers having uniform/normal(Gaussian) distributions, respectively [K-2, Chapter 22].
rand(M,N): Generates an M×N matrix consisting of uniformly distributed random numbers randn(M,N): Generates an M×N matrix consisting of normally distributed random numbers
1.1.8.1 Random Number Having Uniform Distribution
The numbers in a matrix generated by the MATLAB function ‘ rand(M,N)
’ have uniform probability distribution over the interval [0,1], as described by U(0,1). The random number x generated by ‘ rand()
’ has the probability density function:
(1.1.1)
whose value is 1 over [0,1] and 0 elsewhere. The average of this standard uniform number x is
(1.1.2)
and its variance or deviation is
(1.1.3)
If you want another random number y with uniform distribution U(a,b), transform the standard uniform number x as follows:
(1.1.4)