How to Create Diagonal Matrix in Julia?

How to Create Diagonal Matrix in Julia?

  • Julia
  • 5 mins read

To create a diagonal matrix in Julia, you can use the Diagonal function from the LinearAlgebra package. The Diagonal function takes a vector as its argument and returns a square matrix with the elements of the vector along the main diagonal. Below are some examples:

Create Diagonal Matrix in Julia Examples

This code creates a vector v of four values, and then uses the Diagonal function to create a diagonal matrix d from the vector. The resulting matrix has the elements of the vector along the main diagonal, and zeros in all other positions.

using LinearAlgebra

# Create a vector of values
v = [1, 2, 3, 4]

# Create a diagonal matrix from the vector
d = Diagonal(v)

# Print the matrix
println(d)

The matrix is then printed to the console, which produces the following output:

4×4 Diagonal{Int64,Array{Int64,1}}:
 1  ⋅  ⋅  ⋅
 ⋅  2  ⋅  ⋅
 ⋅  ⋅  3  ⋅
 ⋅  ⋅  ⋅  4

In this example, the diagonal matrix has four rows and four columns, since the vector has four elements. The elements of the vector are placed along the main diagonal of the matrix, starting from the upper left corner and moving towards the lower right corner. All other elements of the matrix are set to zero.

You can also use the Diagonal function to create a diagonal matrix with a specific size, by specifying the dimensions of the matrix as additional arguments to the function. For example:

using LinearAlgebra

# Create a vector of values
v = [1, 2, 3, 4]

# Create a 5x5 diagonal matrix from the vector
d = Diagonal(v, 5, 5)

# Print the matrix
println(d)

This code is similar to the previous example, but it creates a 5x5 diagonal matrix instead of a 4x4 matrix. The resulting matrix has the elements of the vector along the main diagonal, with zeros in all other positions. The matrix is then printed to the console, which produces the following output:

5×5 Diagonal{Int64,Array{Int64,1}}:
 1  ⋅  ⋅  ⋅  ⋅
 ⋅  2  ⋅  ⋅  ⋅
 ⋅  ⋅  3  ⋅  ⋅
 ⋅  ⋅  ⋅  4  ⋅
 ⋅  ⋅  ⋅  ⋅  0

In this example, the matrix has five rows and five columns, and the elements of the vector are placed along the main diagonal of the matrix. Since the vector has only four elements, the remaining element of the matrix (in the lower right corner) is set to zero.

Create Diagonal Matrix using Diagm Function in Julia

To create a diagonal matrix in Julia, you can use the diagm function in the LinearAlgebra package. Here is an example of how to use the diagm function to create a diagonal matrix in Julia:

# Load the LinearAlgebra package
using LinearAlgebra

# Create a vector of values to put on the diagonal of the matrix
d = [1, 2, 3, 4]

# Create a diagonal matrix from the vector d
m = diagm(d)

# Print the resulting matrix to the console
println(m)

This code creates a diagonal matrix m from the vector of values d. The matrix m would have the values in the vector d on its diagonal, and all other elements would be zero. The code then prints the resulting matrix m to the console. The output of the program would be:

4×4 Array{Int64,2}:
 1  0  0  0
 0  2  0  0
 0  0  3  0
 0  0  0  4

In this example, the diagm function is used to create the diagonal matrix m from the vector of values d. The diagm function takes the vector d as its argument, and returns a diagonal matrix with the values in d on its diagonal. The code then prints the resulting matrix m to the console using the println function.

You can also use the I function from the LinearAlgebra package to create an identity matrix, which is a square matrix with ones on the main diagonal and zeros in all other positions. The I function takes the dimensions of the matrix as its arguments and returns a matrix with the specified dimensions. For example:

using LinearAlgebra

# Create a 3x3 identity matrix
i = I(3)

# Print the matrix
println(i)

This code creates a 3x3 identity matrix using the I function, and then prints the matrix to the console. The resulting matrix has ones along the main diagonal and zeros in all other positions. The matrix is printed to the console, which produces the following output:

3×3 Diagonal{Bool,Array{Bool,1}}:
 1  ⋅  ⋅
 ⋅  1  ⋅
 ⋅  ⋅  1

In this example, the matrix has three rows and three columns, and the main diagonal of the matrix contains ones. All other elements of the matrix are set to zero. You can use the I function to create identity matrices of any size, by specifying the dimensions of the matrix as arguments to the function.

You can also combine the Diagonal, Diag, and I functions to create more complex diagonal matrices with specific patterns of non-zero elements. For example:

using LinearAlgebra

# Create a vector of values
v = [1, 2, 3, 4]

# Create a 5x5 diagonal matrix from the vector
d1 = Diagonal(v, 5, 5)

# Create a 5x5 identity matrix
i = I(5)

# Create a 5x5 diagonal matrix from the vector, with the diagonal starting at position -1
d2 = Diag(v, -1, 5, 5)

# Create a 5x5 diagonal matrix from the vector, with the diagonal starting at position 1
d3 = Diag(v, 1, 5, 5)

# Create a 5x5 diagonal matrix by adding the three diagonal matrices
d = d1 + d2 + d3

# Print the matrix
println(d)

This code creates three diagonal matrices using the Diagonal, Diag, and I functions, and then combines the matrices into a single matrix by adding them together. The resulting matrix has the elements of the vector along three different diagonals, with ones along the main diagonal and zeros in all other positions. The matrix is then printed to the console.

Related:

  1. How to Create Matrix in Julia?
  2. Convert vector to matrix in Julia
  3. Convert dataframe to matrix in Julia