Page 33 - Applied Numerical Methods Using MATLAB
P. 33
22 MATLAB USAGE AND COMPUTATIONAL ERRORS
>>[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. 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 %90 ◦ rotation
2 5
-1 4
◦
A4 = 2 5 4 %90 x(-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. We can use the reshape() command to change the row-column size of a
matrix with its elements preserved (columnwisely first).
>>A7 = reshape(A1,3,2)
A7=-1 5
4 3
2 2
>>A8 = reshape(A1,6,1), A8 = A1(:) %makes supercolumn vector
A8=-1
4
2
5
3
2
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-1],
Chapter 22).
rand(M,N): generates an MxN matrix consisting of uniformly distributed
random numbers
randn(M,N): generates an MxN matrix consisting of normally distributed
random numbers