Page 28 - Applied Numerical Methods Using MATLAB
P. 28
BASIC OPERATIONS OF MATLAB 17
(A11) Use the length() and size() commands as indicated below.
>>length(D)
ans = 4
>>[M,N] = size(A)
M = 3
N = 3
MATLAB enables us to handle vector/matrix operations in almost the same
way as scalar operations. However, we must make sure of the dimensional com-
patibility between vectors/matrices, and we must put a dot (.) in front of the
operator for termwise (element-by-element) operations. The addition of a matrix
and a scalar adds the scalar to every element of the matrix. The multiplication
of a matrix by a scalar multiplies every element of the matrix by the scalar.
There are several things to know about the matrix division and inversion.
Remark 1.1. Rules of Vector/Matrix Operation
1. For a matrix to be invertible, it must be square and nonsingular; that is, the
numbers of its rows and columns must be equal and its determinant must
not be zero.
2. The MATLAB command pinv(A) provides us with a matrix X of the same
T
dimension as A such that AXA = A and XAX = X. Wecan usethis
T
T −1
command to get the right/left pseudo- (generalized) inverse A [AA ] /
−1
T
[A A] A T for a matrix A given as its input argument, depending on
whether the number (M) of rows is smaller or greater than the number
(N) of columns, so long as the matrix is of full rank; that is, rank(A) =
T
T
T −1
−1
T
min(M, N)[K-1, Section 6.4]. Note that A [AA ] /[A A] A is called
the right/left inverse because it is multiplied onto the right/left side of A
to yield an identity matrix.
3. You should be careful when using the pinv(A) command for a rank-
deficient matrix, because its output is no longer the right/left inverse, which
does not even exist for rank-deficient matrices.
4. The value of a scalar function having an array value as its argument is also
an array with the same dimension.
Supposewehavedefinedvectorsa 1 ,a 2 ,b 1 ,b 2 andmatricesA 1 ,A 2 ,B asfollows:
>>a1 = [-1 2 3]; a2 = [4 5 2]; b1 = [1 -3]’; b2 = [-2 0];
1
a 1 = [ −123 ],a 2 = [ 45 2 ],b 1 = ,b 2 = [ −12 3 ]
−3
>>A1 = [a1;a2], A2 = [a1;[b2 1]], B = [b1 b2’]
−123 −123 1 −2
A 1 = , A 2 = , B =
452 −201 −3 0