Chapter 5 Matrix Basics
5.1 Laws of Matrix Algebra
Go back to fan’s MEconTools Package, Matlab Code Examples Repository (bookdown site), or Math for Econ with Matlab Repository (bookdown site).
5.1.1 6 Old Rules, 5 Still Apply
We had associative, commutative and distributive laws for scalar algebra, we can think of them as the six bullet points below. Only the multiplicative-commutative law no longer works for matrix, the other rules work for matrix as well as scalar algebra.
Associative laws work as in scalar algebra for matrix
\(\displaystyle (A+B)+C=A+(B+C)\)
\(\displaystyle (A\cdot B)\cdot C=A\cdot (B\cdot C)\)
Commutative Law works as well for addition
\(\displaystyle A+B=B+A\)
with scalars, we know \(3\cdot 4=4\cdot 3\), but commutative law for matrix multiplication does not work, Matrix \(A\cdot B\not= B\cdot A\). The matrix dimensions might not even match up for multiplication. (see below for examples)
And Distributive Law still applies to matrix
\(\displaystyle A\cdot (B+C)=A\cdot B+A\cdot C\)
\(\displaystyle (B+C)\cdot A=B\cdot A+C\cdot A\)
5.1.2 Example for \(A\cdot B\not= B\cdot A\)
% Non-Square
A = rand(2,3)
A = 2x3
0.6959 0.6385 0.0688
0.6999 0.0336 0.3196
B = rand(3,4)
B = 3x4
0.5309 0.8200 0.5313 0.6110
0.6544 0.7184 0.3251 0.7788
0.4076 0.9686 0.1056 0.4235
% This is OK
disp(A*B)
0.8154 1.0960 0.5847 0.9516
0.5238 0.9076 0.4166 0.5891
% This does not work
try
B*A
catch ME
disp('does not work! Dimension mismatch')
end
does not work! Dimension mismatch
% Square
A = rand(3,3)
A = 3x3
0.0908 0.2810 0.4574
0.2665 0.4401 0.8754
0.1537 0.5271 0.5181
B = rand(3,3)
B = 3x3
0.9436 0.2407 0.6718
0.6377 0.6761 0.6951
0.9577 0.2891 0.0680
% This is OK
A*B
ans = 3x3
0.7030 0.3441 0.2875
1.3704 0.6147 0.5445
0.9773 0.5431 0.5049
% This works, but result differs from A*B
B*A
ans = 3x3
0.2531 0.7252 0.9904
0.3449 0.8432 1.2437
0.1745 0.4322 0.7263
5.1.3 4 New Rules for Transpose
In scalar algebra, transpose does not make sense. Given matrix \(A\), \(A^T\) is the transpose matrix of \(A\) where each row of \(A\) becomes columns in \(A^T\). If \(A\) is \(M\) by \(N\), then \(A^T\) is \(N\) by \(M\).
Given matrix \(A\) and scalar value \(r\):
1: \((r\cdot A)^T =r\cdot A^T\)
2: \((A^T )^T =A\)
3: \((A+B)^T =A^T +B^T\)
4: \((A\cdot B)^T =B^T \cdot A^T\)
For the 4th rule, suppose matrix \(A\) is has \(L\) rows and \(M\) columns, and the matrix \(B\) has \(M\) rows and \(N\)columns. \((A\cdot B)\) is a \(L\) by \(N\) matrix, \((A\cdot B)^T\) is a \(N\) by \(L\) matrix. This is equal to \(B^T \cdot A^T\), where we have a \(N\) by \(M\) matrix \(B^T\) multiplied by a \(M\) by \(L\) matrix \(A^T\), and the resulting matrix is \(N\) by \(L\).
A = rand(2,3)
A = 2x3
0.2548 0.6678 0.3445
0.2240 0.8444 0.7805
Atranspose = (A')
Atranspose = 3x2
0.2548 0.2240
0.6678 0.8444
0.3445 0.7805
5.2 Matrix Addition and Multiplication
Go back to fan’s MEconTools Package, Matlab Code Examples Repository (bookdown site), or Math for Econ with Matlab Repository (bookdown site).
5.2.1 Scalar Multiplication/Division, Addition/Subtraction
If we multiply a matrix by a number, we multiply every element of that matrix by that number. Addition, subtraction, and division of a matrix with a sclar value work the same way
c = 10
c = 10
matA = rand(3,2)
matA = 3x2
0.3111 0.1848
0.9234 0.9049
0.4302 0.9797
c*matA
ans = 3x2
3.1110 1.8482
9.2338 9.0488
4.3021 9.7975
matA/c
ans = 3x2
0.0311 0.0185
0.0923 0.0905
0.0430 0.0980
matA - c
ans = 3x2
-9.6889 -9.8152
-9.0766 -9.0951
-9.5698 -9.0203
matA + c
ans = 3x2
10.3111 10.1848
10.9234 10.9049
10.4302 10.9797
5.2.2 Addition and Subtraction
You can add/subtract together two matrixes of the same size. We can add up the two 3 by 1 vectors from above, and the two 2 by 3 matrixes from above.
colVecA = rand(3,1)
colVecA = 3x1
0.4389
0.1111
0.2581
colVecB = rand(3,1)
colVecB = 3x1
0.4087
0.5949
0.2622
matA = rand(3,2)
matA = 3x2
0.6028 0.1174
0.7112 0.2967
0.2217 0.3188
matB = rand(3,2)
matB = 3x2
0.4242 0.2625
0.5079 0.8010
0.0855 0.0292
colVecA + colVecB
ans = 3x1
0.8476
0.7060
0.5203
matA - matB
ans = 3x2
0.1787 -0.1451
0.2034 -0.5043
0.1362 0.2896
When using matlab, even if you add up to a single column or single row with a matrix that has multiple rows and columns, if the column count or row count matches up, matlab will broadcast rules, and addition will still be legal. In the example below, matA is 3 by 2, and colVecA is 3 by 1, matlab duplicate colVecA and add it to each column of matA (Broadcast rules are important for efficient storage and computation):
matA + colVecA
ans = 3x2
1.0417 0.5563
0.8223 0.4078
0.4798 0.5768
5.2.3 Matrix Multiplication
When we try to multiply two matrixes together: \(A\cdot B\) for example, the number of columns of matrix \(A\) and the number of rows of matrix \(B\) have to match up.
If the matrix \(A\) is has \(L\) rows and \(M\) columns, and the matrix \(B\) has \(M\) rows and \(N\)columns, then the resulting matrix of \(C=A\cdot B\) has to have \(L\) rows and \(N\) columns.
Each of the \((l,n)\) cell in the product matrix \(C=A\cdot B\), is equal to:
- \(\displaystyle C_{l,n} =\sum_{m=1}^M A_{l,m} \cdot B_{m,n}\)
Note that we are summing over \(M\): row \(l\) in matrix \(A\), and column \(n\) in matrix \(B\) both have \(M\) elements. We multiply each \(m\) of the \(M\) element from the row in \(A\) and column in \(B\) together one by one, and then sum them up to end up with the value for the \(l\)th row and \(n\)th column in matrix \(C\).
% (3 by 4) times (4 by 2) end up with (3 by 2)
L = 3;
M = 4;
N = 2;
matA = rand(L, M)
matA = 3x4
0.9289 0.5785 0.9631 0.2316
0.7303 0.2373 0.5468 0.4889
0.4886 0.4588 0.5211 0.6241
matB = rand(M, N)
matB = 4x2
0.6791 0.0377
0.3955 0.8852
0.3674 0.9133
0.9880 0.7962
matC = matA*matB
matC = 3x2
1.4423 1.6111
1.2738 1.1262
1.3214 1.3974
% (2 by 10) times (10 by 1) end up with (2 by 1)
L = 2;
M = 10;
N = 1;
matA = rand(L, M)
matA = 2x10
0.0987 0.3354 0.1366 0.1068 0.4942 0.7150 0.8909 0.6987 0.0305 0.5000
0.2619 0.6797 0.7212 0.6538 0.7791 0.9037 0.3342 0.1978 0.7441 0.4799
matB = rand(M, N)
matB = 10x1
0.9047
0.6099
0.6177
0.8594
0.8055
0.5767
0.1829
0.2399
0.8865
0.0287
matC = matA*matB
matC = 2x1
1.6524
3.5895
5.3 Creating Matrixes in Matlab
Go back to fan’s MEconTools Package, Matlab Code Examples Repository (bookdown site), or Math for Econ with Matlab Repository (bookdown site).
5.3.1 Matlab Define Row and Column Vectors (Matrix)
% A column vector 4 by 1, with three numbers you fill in by yourself
colVec = [5;2;3;10]
colVec = 4x1
5
2
3
10
% Another column vector with 4 random numbers
colVecRand = rand(4,1)
colVecRand = 4x1
0.4899
0.1679
0.9787
0.7127
% A row vector 1 by 4
rowVec = [3,2,4,5]
rowVec = 1x4
3 2 4 5
% A row vector 1 by 4 with random number
rowVecRand = rand(1,4)
rowVecRand = 1x4
0.5005 0.4711 0.0596 0.6820
5.3.2 Matlab Define a Matrix
% A 2 by 3 matrix by hand
matA = [1,2,1;
3,4,10]
matA = 2x3
1 2 1
3 4 10
% Another 2 by 3 matrix, now with random numbers
matRand = rand(2,3)
matRand = 2x3
0.0424 0.5216 0.8181
0.0714 0.0967 0.8175
% Another 2 by 3 matrix, now with random integers between 1 and 10
% rand draws between 0 and 1, ceil converts 0.1 to 1, 1.1 to 2, etc
matRand = ceil(rand(2,3)*10)
matRand = 2x3
8 7 10
2 6 7
5.3.3 Matlab Define a Square Matrix
% A 4 by 4 square matrix
matSquare = rand(4)
matSquare = 4x4
0.8003 0.0835 0.8314 0.5269
0.4538 0.1332 0.8034 0.4168
0.4324 0.1734 0.0605 0.6569
0.8253 0.3909 0.3993 0.6280
% or can define 4 by 4
matSquare = rand(4, 4)
matSquare = 4x4
0.2920 0.1672 0.4897 0.0527
0.4317 0.1062 0.3395 0.7379
0.0155 0.3724 0.9516 0.2691
0.9841 0.1981 0.9203 0.4228
% or can define 4 by 4, between 1 and 5 each number
matSquare = ceil(rand(4, 4)*5)
matSquare = 4x4
3 2 4 5
5 4 4 1
3 4 1 1
5 3 1 3
5.3.4 Identity Matrix
If a matrix \(A\) is square matrix with the same number of rows and columns, and all diagonal elements are \(1\) and non-diagonal elements are \(0\), then \(A\) is an identity matrix:
\(A_{i,j}\) are the value in the ith row and jth column of the matrix \(A\)
\(A\) is an identity matrix, when: \(A_{i,j} =0\;\textrm{if}\;i\not= j\), \(A_{i,j} =1\;\textrm{if}\;i=j\)
% 4 by 4 identity matrix
identity4by4 = eye(4)
identity4by4 = 4x4
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
When a matrix is muplieid by the identity matrix, you get the same matrix back, for example, multiplying random integer 4 by 4 matrix by the 4 by 4 identity matrix:
matSquare
matSquare = 4x4
3 2 4 5
5 4 4 1
3 4 1 1
5 3 1 3
matSquareTimesIdentity = matSquare*identity4by4
matSquareTimesIdentity = 4x4
3 2 4 5
5 4 4 1
3 4 1 1
5 3 1 3
When a row vector is muplieid by the identity matrix, you get the same vector back, for example, multiplying random integer 1 by 4 row vector by the 4 by 4 identity matrix:
rowVec
rowVec = 1x4
3 2 4 5
rowVecTimesIdentity = rowVec*identity4by4
rowVecTimesIdentity = 1x4
3 2 4 5
When an identity matrix is multiplied by a column vector, you get the same vector back, for example, multiplying 4 by 4 identity matrix by random integer 4 by 1 column vector by the :
colVec
colVec = 4x1
5
2
3
10
colVecTimesIdentity = identity4by4*colVec
colVecTimesIdentity = 4x1
5
2
3
10
5.3.5 Lower-Triangular Matrix and Upper-Triangular Matrix
A lower triangular matrix is a square matrix where:
Square matrix\(A\) is a lower triangular matrix, when: \(A_{i,j} =0\;\textrm{if}\;i<j\)
Square matrix\(A\) is a upper triangular matrix, when: \(A_{i,j} =0\;\textrm{if}\;i>j\)
% lower triangular matrix of matA
lowerTriangular = tril(matSquare)
lowerTriangular = 4x4
3 0 0 0
5 4 0 0
3 4 1 0
5 3 1 3
% upper triangular matrix of matA
upperTriangular = triu(matSquare)
upperTriangular = 4x4
3 2 4 5
0 4 4 1
0 0 1 1
0 0 0 3
5.3.6 Three Dimensions Matrix (Tensor)
% 3 by 3 by 2, storing multiple matrixes together in tenA
tenA = zeros(3,3,2);
tenA(:,:,1) = rand(3,3);
tenA(:,:,2) = rand(3,3);
disp(tenA);
(:,:,1) =
0.8819 0.3689 0.1564
0.6692 0.4607 0.8555
0.1904 0.9816 0.6448
(:,:,2) =
0.3763 0.4820 0.2262
0.1909 0.1206 0.3846
0.4283 0.5895 0.5830
% Creating four 2 by 3 matrixes
matRand = rand(2,3,4)
matRand =
matRand(:,:,1) =
0.2518 0.6171 0.8244
0.2904 0.2653 0.9827
matRand(:,:,2) =
0.7302 0.5841 0.9063
0.3439 0.1078 0.8797
matRand(:,:,3) =
0.8178 0.5944 0.4253
0.2607 0.0225 0.3127
matRand(:,:,4) =
0.1615 0.4229 0.5985
0.1788 0.0942 0.4709
disp(matRand);
(:,:,1) =
0.2518 0.6171 0.8244
0.2904 0.2653 0.9827
(:,:,2) =
0.7302 0.5841 0.9063
0.3439 0.1078 0.8797
(:,:,3) =
0.8178 0.5944 0.4253
0.2607 0.0225 0.3127
(:,:,4) =
0.1615 0.4229 0.5985
0.1788 0.0942 0.4709