% create a matrix just like vectors - use semicolons to start new row
format compact
matrix1 = [1 2;3 4]
matrix1 = 1 2 3 4
% another example - 3x3 this time
matrix2 = [1 2 3; 4 5 6; 7 8 9]
matrix2 = 1 2 3 4 5 6 7 8 9
% to access elements, use matrix(m,n)
matrix2(2,3)
ans = 6
clear all
matrix3 = [3 7 4 2; 6 5 1 8; 4 7 9 10; 23 6 45 89]
matrix3 = 3 7 4 2 6 5 1 8 4 7 9 10 23 6 45 89
% extract a specific section of matrix with matrix(row:row, col:col)
% here, we want a 2x2 matrix from the bottom right corner
matrix3(3:4,3:4)
ans = 9 10 45 89
% if we want a specific range from one row, ex. row 3 cols 2-4
matrix3(3,2:4)
ans = 7 9 10
% must be the same size for addition
clear all
a = [1 2; 3 4];
b = [5 6; 8 1];
c = a + b
c = 6 8 11 5
c = a - b
c = -4 -4 -5 3
% to multiply by a constant
a * 4
ans = 4 8 12 16
% divide by constant
ans / 4
ans = 1 2 3 4
% basic matrix multiplication
a * b
ans = 21 8 47 22
% element-by-element multiplication - use the dot/period
a = [1 0; 0 1]
b = [2 0; 0 2]
a.*b
a = 1 0 0 1 b = 2 0 0 2 ans = 2 0 0 2
% when we use sum(matrix) the columns are added - the result is a vector
clear all
a = [1 2 3; 4 5 6; 7 8 9]
sum(a)
a = 1 2 3 4 5 6 7 8 9 ans = 12 15 18
% if we want the sum of all elements in a matrix, we can nest the operations
sum(sum(a))
ans = 45
% much like sum(matrix), the min(matrix) function works the same way
% the minimum value of each COLUMN is retured as a vector
clear all
a = [-5 78 345 90; 578 -300 189 3; -4 89 989 1000]
min(a)
a = -5 78 345 90 578 -300 189 3 -4 89 989 1000 ans = -5 -300 189 3
% again, by nesting min(min()), we can get the smallest value for a given matrix
min(min(a))
ans = -300
% max(matrix) works exactly the same
max(a)
ans = 578 89 989 1000
% nesting max
max(max(a))
ans = 1000
% calling size(matrix) will return the NxM (rows x cols) dimensions
clear all
a = [1 2 3 4 5; 6 7 8 9 10]
size(a)
a = 1 2 3 4 5 6 7 8 9 10 ans = 2 5
% there is another function that will return the number of elements - numel(matrix)
numel(a)
ans = 10
% we can combine multiple matrices, but they must be of the same dimensions
clear all
a = [1 2 3 4 5; 6 7 8 9 10]
b = [11 12 13; 14 15 16]
c = [a;b]
Dimensions of matrices being concatenated are not consistent.
% correct the dimensions for b and it works
b = [11 12 13 14 15; 16 17 18 19 20]
c = [a;b]
b = 11 12 13 14 15 16 17 18 19 20 c = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Calling functions such sin(x), log(x), etc. on a variable will operate the function as expected. But with matrices, many functions can be applied to the entire matrix or to each element.
% if we call sqrt(matrix) we get the square root of each element
A = [1 4 9; 16 25 36; 49 64 81]
sqrt(A)
A = 1 4 9 16 25 36 49 64 81 ans = 1 2 3 4 5 6 7 8 9
% however, using sqrtm(matrix) will give us the matrix R that if multipled by R would give us A
R = sqrtm(A)
R = 0.6344 + 1.3620i 0.3688 + 0.7235i 0.7983 - 0.4388i 1.4489 + 1.1717i 2.7697 + 0.6224i 3.2141 - 0.3775i 4.3578 - 1.6237i 5.7110 - 0.8625i 7.7767 + 0.5231i
R*R
ans = 1.0000 - 0.0000i 4.0000 - 0.0000i 9.0000 - 0.0000i 16.0000 - 0.0000i 25.0000 - 0.0000i 36.0000 - 0.0000i 49.0000 + 0.0000i 64.0000 + 0.0000i 81.0000 + 0.0000i
% the result is still in complex, but notice that the imaginary coefficients are all zero
% we can get the output back to the way it was....
round(real(R*R))
ans = 1 4 9 16 25 36 49 64 81
% NOTE: sqrtm(matrix) can only be performed on MxM (rows=cols) matrices!
B = [1 2 3; 4 5 6];
sqrtm(B)
Expected input to be a square matrix.
% exponential
exp(A)
ans = 1.0e+35 * 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 1.5061
% note the difference in output
expm(A)
ans = 1.0e+48 * 0.4442 0.6130 0.8149 1.9817 2.7349 3.6359 4.6459 6.4116 8.5240
% log
log(A)
ans = 0 1.3863 2.1972 2.7726 3.2189 3.5835 3.8918 4.1589 4.3944
% again, the output is NOT the same
logm(A)
[Warning: Principal matrix logarithm is not defined for A with nonpositive real eigenvalues. A non-principal matrix logarithm is returned.] [> In logm (line 78) In pymat_eval (line 31) In matlabserver (line 24)] ans = 0.6794 + 1.7064i 1.1840 + 0.9064i -0.1180 - 0.5498i 2.4176 + 1.4680i 0.9442 + 0.7798i 1.3825 - 0.4730i 0.3878 - 2.0343i 2.2002 - 1.0806i 3.7517 + 0.6554i
There are notable differences when using matrices and powers. Given matrix A and any number n, we can perform the following operations:
% scenario 1: raising 2 to the power of A
A = [1 2 3; 4 5 6; 7 8 9]
2^A
A = 1 2 3 4 5 6 7 8 9 ans = 1.0e+04 * 0.7962 0.9782 1.1603 1.8029 2.2154 2.6276 2.8097 3.4523 4.0950
% scenario 2: raising n to the power of each element in A — note the dot/period
2.^A
ans = 2 4 8 16 32 64 128 256 512
% scenario 3: raising A to the power of 2
A^2
ans = 30 36 42 66 81 96 102 126 150
% scenario 4: raising each element in A to the power of 2 - note the dot/period
A.^2
ans = 1 4 9 16 25 36 49 64 81
Many matrix calculations require the use of special matrices populated with specific values. Some special-purpose matrices include all zeros, all ones, and diagonal ones (identity).
% zeros(N) will produce an NxN matrix populated with zeros
Z = zeros(3)
Z = 0 0 0 0 0 0 0 0 0
% the same applies to creating an NxN matix of ones
O = ones(5)
O = 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
% to specify an NxM matrix, we call ones(N,M) and zeros(N,M) where N = rows and M = cols
O = ones(2,5)
Z = zeros(4,2)
O = 1 1 1 1 1 1 1 1 1 1 Z = 0 0 0 0 0 0 0 0
% to create the identity matrix (diagonal ones) we use eye(N)/eye(N,M)
format compact
eye(3)
ans = 1 0 0 0 1 0 0 0 1
% note that if the parameters are not NxN, MATLAB still tries to do it
eye(3,4)
ans = 1 0 0 0 0 1 0 0 0 0 1 0
% multiplying A by identity gives A
A = [1 2 3; 4 5 6; 7 8 9]
A*eye(3)
A = 1 2 3 4 5 6 7 8 9 ans = 1 2 3 4 5 6 7 8 9
% magic(N) creates a NxN matrix where the sums of each row and each column are equal
magic(3)
ans = 8 1 6 3 5 7 4 9 2
% recall that for vectors, which are actually matrices, we use the single-quote to create transpose
V = [1 2 3 4 5]
V'
V = 1 2 3 4 5 ans = 1 2 3 4 5
% creating a transpose of an NxM matrix will return a MxN transposed matrix
A = [1 2 3 4; 5 6 7 8]
A'
A = 1 2 3 4 5 6 7 8 ans = 1 5 2 6 3 7 4 8
% to extract the diagonal of an NxN matrix, we use diag(A)
A = [1 2 3 4; 5 6 7 8; 9 10 11 12]
diag(A)
diag(A)'
A = 1 2 3 4 5 6 7 8 9 10 11 12 ans = 1 6 11 ans = 1 6 11
% we use these to zero-out the portion we are not interested in above/below the diagonal
% if we want the upper portion of A
A = [1 2 3 4; 5 6 7 8; 9 10 11 12; 13 14 15 16]
triu(A)
A = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ans = 1 2 3 4 0 6 7 8 0 0 11 12 0 0 0 16
% the same goes for the lower half
tril(A)
ans = 1 0 0 0 5 6 0 0 9 10 11 0 13 14 15 16
Recall that systems of equations can be solved using matrices. Given the following system, we can model it as a 2x3 matrix. \begin{equation}x+4y=-3\end{equation} \begin{equation}6x+2y=8\end{equation}
% remember to align each variable in the same cols - here, x is col 1, y is col 2 and numeric solution col 3
A = [1 4 -3; 6 2 8]
A = 1 4 -3 6 2 8
To solve the system, we use reduced-row echelon form rref()
% pass the system matrix to rref()
rref(A)
ans = 1.0000 0 1.7273 0 1.0000 -1.1818
In the result, there is a coeffecient of 1 for x and y; in the answer column we have numeric results. So the solution to the system is\begin{equation}x=1.7273\end{equation}\begin{equation}y=-1.1818\end{equation}
% the trace is the sum of the diagonal
A = [1 2 3; 4 5 6; 7 8 9]
trace(A)
A = 1 2 3 4 5 6 7 8 9 ans = 15
% rank is called by rank(A)
rank(A)
ans = 2
% the determinant is another useful function provided by MATLAB
det(A)
ans = 6.6613e-16
% again, the inverse is often calculated for matrices
A = [1 2 3; 4 5 6; 6 -1 0]
inv(A)
A = 1 2 3 4 5 6 6 -1 0 ans = -0.2500 0.1250 0.1250 -1.5000 0.7500 -0.2500 1.4167 -0.5417 0.1250
% clear and start some symbolic variables
clear all
x = sym('x'); % longer workaround - Matlab kernel in Jupyter notebook not allowing syms() function
A = sym('A');
B = sym('B');
A = [2-x 3*x^3; 4*x^2-3*x 9*x^3+2]
B = [2*x-x^3 8*x+1; x-x^3 5*x+8*x^3]
A = [ 2 - x, 3*x^3] [ 4*x^2 - 3*x, 9*x^3 + 2] B = [ - x^3 + 2*x, 8*x + 1] [ - x^3 + x, 8*x^3 + 5*x]
% lets clean it up!
pretty(A)
pretty(B)
/ 3 \ | 2 - x, 3 x | | | | 2 3 | \ 4 x - 3 x, 9 x + 2 / / 3 \ | - x + 2 x, 8 x + 1 | | | | 3 3 | \ - x + x, 8 x + 5 x /
% we can perform many of the previous functions on symbolics
pretty(inv(A))
/ 3 3 \ | 9 x + 2 3 x | | - --------, ---- | | #1 #1 | | | | 2 | | - 4 x + 3 x x - 2 | | - ------------, ----- | \ #1 #1 / where 5 3 #1 == (6 x - 9 x + x - 2) 2
pretty(diag(B))
/ 3 \ | - x + 2 x | | | | 3 | \ 8 x + 5 x /