Julia and Matrices

For the most part we’ll use LinearAlgebra package.

using LinearAlgebra

Initializing matrices

Setting type

You can initialize a float 64 matrix in the following way.

m = Matrix{Float64}([1 1;2 2])
2×2 Matrix{Float64}:
 1.0  1.0
 2.0  2.0

Identity and diagonal matrices

I represents an identity matrix. You don’t really need to specify its dimensions.

3* I + [1 1 1; 1 1 1; 1 1 1]
3×3 Matrix{Int64}:
 4  1  1
 1  4  1
 1  1  4

For a diagonal matrix with different entries along the diagonal:

Extraction from matrices

You can extract the diagonal from a matrix. Note that c[2,1] is 0. Note the lack of commas.

c  = Diagonal([1 2 3; 2 3 4; 3 7 8])
c[2,1]
0

To convert a vector (note the commas) into a diagonal matrix use diagm:

a = [1, 2, 3, 4]
Diagonal(a)
4×4 Diagonal{Int64, Vector{Int64}}:
 1  ⋅  ⋅  ⋅
 ⋅  2  ⋅  ⋅
 ⋅  ⋅  3  ⋅
 ⋅  ⋅  ⋅  4

For a true diagonal matrix (again, no commas)

diagm(a)
4×4 Matrix{Int64}:
 1  0  0  0
 0  2  0  0
 0  0  3  0
 0  0  0  4

List comprehension