Engee documentation
Notebook

Matrices and arrays

The Julia programming language provides a wide range of possibilities for creating and editing matrices. In this example some of them will be considered.

To create a matrix consisting of several rows, divide the matrix elements by semicolon:

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

Julia allows you to process all values in a matrix with a single arithmetic operator or function by specifying a dot in front of it, which means that the operator is applied element by element:

In [ ]:
a .+ 10
Out[0]:
3×3 Matrix{Int64}:
 11  13  15
 12  14  16
 17  18  20

Element-by-element degree expansion:

In [ ]:
a .^ 3
Out[0]:
3×3 Matrix{Int64}:
   1   27   125
   8   64   216
 343  512  1000

You can also apply functions to matrices:

In [ ]:
sin(a)
Out[0]:
3×3 Matrix{Float64}:
 -0.657343  -0.275398   0.014688
 -0.572697  -0.24759   -0.144781
  0.254022  -0.38759   -0.874514

The transpose of a matrix can be done in two ways, the first is by specifying an apostrophe:

In [ ]:
a'
Out[0]:
3×3 adjoint(::Matrix{Int64}) with eltype Int64:
 1  2   7
 3  4   8
 5  6  10

The second way is by using the transpose function:

In [ ]:
transpose(a)
Out[0]:
3×3 transpose(::Matrix{Int64}) with eltype Int64:
 1  2   7
 3  4   8
 5  6  10

The concatenation of matrices is done by specifying them in square brackets, with or without semicolon, which means vertical and horizontal addition, respectively:

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

Concatenation of matrices horizontally:

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

You can also use complex numbers as array elements by defining them through im:

In [ ]:
c = [3+4im 4+3im; -im 10im]
Out[0]:
2×2 Matrix{Complex{Int64}}:
 3+4im  4+3im
 0-1im  0+10im