Engee documentation
Notebook

Basic operations with matrices

A matrix is a two-dimensional rectangular array of elements arranged in rows and columns. The elements can be numbers, boolean values (true or false), date and time, strings, categorical values, or some other type of data.

Defining matrices

The matrix can be created using square brackets [], enclosing the necessary elements in them in advance.

In [ ]:
Pkg.add(["LinearAlgebra"])
In [ ]:
A = [1 2 3] # создание матрицы в виде вектор-строки
Out[0]:
1×3 Matrix{Int64}:
 1  2  3

A matrix with one column will be displayed as another data type, a vector.:

In [ ]:
A = [1, 2, 3]
Out[0]:
3-element Vector{Int64}:
 1
 2
 3

A regular rectangular matrix is created by defining the rows and separating them using a semicolon.:

In [ ]:
A = [1 2 3 4; 5 6 7 8]
Out[0]:
2×4 Matrix{Int64}:
 1  2  3  4
 5  6  7  8

There are many functions that help you create matrices with specific element values or a specific structure. For example, the functions zeros and ones create matrices of all zeros or ones. The first and second arguments of these functions are the number of rows and the number of columns of the matrix.:

In [ ]:
A = zeros(3, 3) # определение матрицы с нулями
Out[0]:
3×3 Matrix{Float64}:
 0.0  0.0  0.0
 0.0  0.0  0.0
 0.0  0.0  0.0
In [ ]:
A = ones(3, 3) # определение матрицы с единицами
Out[0]:
3×3 Matrix{Float64}:
 1.0  1.0  1.0
 1.0  1.0  1.0
 1.0  1.0  1.0

The Diagonal function of the LinearAlgebra library places input elements on the diagonal of the array:

In [ ]:
using LinearAlgebra
A = [12, 62, 93, -8]
B = Diagonal(A)
Out[0]:
4×4 Diagonal{Int64, Vector{Int64}}:
 12   ⋅   ⋅   ⋅
  ⋅  62   ⋅   ⋅
  ⋅   ⋅  93   ⋅
  ⋅   ⋅   ⋅  -8

Using the convert function, we convert matrix B from the Diagonal type to the Matrix{Float64} data type

In [ ]:
B = convert(Matrix{Float64}, B)
Out[0]:
4×4 Matrix{Float64}:
 12.0   0.0   0.0   0.0
  0.0  62.0   0.0   0.0
  0.0   0.0  93.0   0.0
  0.0   0.0   0.0  -8.0

Combining matrices

Matrices can be combined horizontally using square brackets [ ]:

In [ ]:
A = [1 2; 3 4; 5 6; 7 8]
C = [A B]
Out[0]:
4×6 Matrix{Float64}:
 1.0  2.0  12.0   0.0   0.0   0.0
 3.0  4.0   0.0  62.0   0.0   0.0
 5.0  6.0   0.0   0.0  93.0   0.0
 7.0  8.0   0.0   0.0   0.0  -8.0

Similarly, matrices can be combined vertically (they must be of the same dimension) by specifying a semicolon.:

In [ ]:
A = ones(1,4)
B = zeros(1,4)
D = [A; B]
Out[0]:
2×4 Matrix{Float64}:
 1.0  1.0  1.0  1.0
 0.0  0.0  0.0  0.0

Special functions such as vcat and hcat can be used to combine them.:

In [ ]:
C = hcat(A, B)
Out[0]:
1×8 Matrix{Float64}:
 1.0  1.0  1.0  1.0  0.0  0.0  0.0  0.0
In [ ]:
D = vcat(A, B)
Out[0]:
2×4 Matrix{Float64}:
 1.0  1.0  1.0  1.0
 0.0  0.0  0.0  0.0

Generating numeric sequences

Using the collect function, you can generate a numeric sequence as a vector.:

In [ ]:
E = collect(-10:5:10) # collect(Начало:Интервал:Конец) 
Out[0]:
5-element Vector{Int64}:
 -10
  -5
   0
   5
  10

Changing the shape of the matrix

To change the dimension of the matrix, you can use the reshape function.

In [ ]:
A = [1 2 3 4 5; 6 7 8 9 10; 11 12 13 14 15; 16 17 18 19 20] # определяем матрицу, исходный размер 4 на 5
Out[0]:
4×5 Matrix{Int64}:
  1   2   3   4   5
  6   7   8   9  10
 11  12  13  14  15
 16  17  18  19  20

Converting the matrix into a string vector:

In [ ]:
reshape(A, (1, 20)) 
Out[0]:
1×20 Matrix{Int64}:
 1  6  11  16  2  7  12  17  3  8  13  18  4  9  14  19  5  10  15  20

Converting the matrix into a column vector:

In [ ]:
reshape(A, (20, 1)) 
Out[0]:
20×1 Matrix{Int64}:
  1
  6
 11
 16
  2
  7
 12
 17
  3
  8
 13
 18
  4
  9
 14
 19
  5
 10
 15
 20

When specifying one of the matrix sizes using a colon, reshape returns a matrix with the specified number of rows or columns.:

In [ ]:
reshape(A, (1, :)) 
Out[0]:
1×20 Matrix{Int64}:
 1  6  11  16  2  7  12  17  3  8  13  18  4  9  14  19  5  10  15  20