Linear Algebra Octave Flashcards
Initialize a vector
V1 = [1 2 3]
Addition of two vectors
v1 + v2
Transpose of a vector
- v’ (hermitian)
- v.’ - regular transpose
- transpose(v) - regular transpose
Scalar multiplication
2 * v
Dot product approaches
- dp = sum (v1 .* v2)
- dp = dot(v1, v2)
- dp = v1T * v2 (v1 and v2 must be column vectors)
Create one random number
randn / randn()
Create random vector of size 10
v = randn(10, 1)
Create a matrix of size 4 X 6
mat = randn(4, 6)
Get the size of the matrix
size(mat) = (4, 6)
Just get the number of rows of a matrix
size(mat, 1) = 4
Just get the number of columns of a matrix
size(mat, 2) = 6 ; length(mat) = 6
Get the first column of a matrix
mat(:, 1)
Get the first and third columns of a matrix
mat(:, [1,3])
Get the one to third columns of a matrix
mat(:, [1:3])
Repeat 1st column twice in a matrix
mat(:, [1,1])
Get the first row of a matrix
mat(1, :)
Get the first and third rows of a matrix
mat([1,3], :)
Get 1 to third rows in a matrix
mat([1:3], :)
Repeat 1st row twice in a matrix
mat([1,1], :)
Repeat 1st row n times in a matrix
mat(repelem(1,n), :)
Create a zero vector of size 4
zero_vector = zeros(4,1)
magnitude of a vector
- sqrt(dot(v,v))
- sqrt(sum(v .* v))
- norm(v)
Angle between two vectors
theta = acos(dot(v1, v2)/(norm(v1) * norm(v2)))
How to know if two vectors are orthogonal
If the dot product of the two vectors is 0
How to create just positive random number
rand
create duplicate dependent vector from v1
v2 = randn/rand * v1
absolute value of a number
abs(-1)
Perform hadamard multiplication
- v1 . * v2
What does v1 * v2 represent
matrix multiplication
Outer product
v .* w’
cross product of vectors
cross(v, w)
create complex number
complex(3, 4) = 3+4i
Create complex vector
v = [3 4i complex(5,2) 2-5i]
transpose(3 + 4i) =
3 + 4i
Hermitian transpose of v = 3+4i - write syntax
v’ * v = 25
Unit vectors - mu =
1/norm(v)
Normalize a vector
mu * v = norm_vector such that norm(norm_vector) = 1
Creating an identity matrix
m = eye(5). Can create rectangular matrices as well. 1’s is equal to number of columns. But identity matrices are only square matrices in practice
Creating an zeros matrix
m = zeros(4). Can create rectangular matrices as well
creating diagonal matrix
m = diag([1 2 3 4 5]); Can create rectangular matrices as well
Creating upper triangular matrix
s = randn(5)
ut = triu(s); Can create rectangular matrices as well
Creating lower triangular matrix
s = randn(5)
lt = tril(s); Can create rectangular matrices as well
Concatenate two matrices
m1 = randn(5* 3)
m2 = randn(5 * 5)
C = [m1 m2]
Addition of matrices
A+B ; both are of same dimensions
Shifting a matrix
D = randn(10)
shft = D * (0.5 * eye(10))
Two applications of diag keyword
- diag(matrix) =pulls out the diagnoal as a row vector, [1;2;3;4;5;6]
- diag(vector) = creates a diagonal matrix with the vector elements as diagonal.
Calculating trace
- trace(matrix)
- sum(diag(matrix))
Properties that prove trace is a linear operator
- trace(a+b) = trace(a) + trace(b)
- l * trace(a) = trace(l*a)
closed under both addition and subtraction
Create a matrix with values from 1 to 12 and shape 3X4
A = reshape(1:12, 3,4)
Broadcasting three ways
a =reshape(1:12, 3,4)
r = [1 2 3 4]
c = [100 200 300]’
- a + repmat(r, size(a,1), 1)
a + repmat(c, 1, size(a,2))) - bsxfun(@plus, a, r)
bsxfun(@plus, a, c) - a + r
a + c
bsxfun stands for
binary expansion
Creating a symmetric matrix from a non-symmetric matrix a
s = (a * a’)/m^2
Hadamard matrix multiplication
.* (* for regular multiplication)
Hadamard multiplication rule
Both the matrices must be of same size