Arrays

An array is a multi-dimensional collection of objects. The elements of arrays do not need to be numbers or even from the same type. However, we are interested in numeric arrays.

using LinearAlgebra
import Random

Random.seed!(11)
Random.TaskLocalRNG()

Basic syntax

Vectors

x = [1, 3, 4]
3-element Vector{Int64}:
 1
 3
 4

Julia allows to perform operations that are almost globally accepted on vectors. For example, let's get the transpose:

x'
1×3 adjoint(::Vector{Int64}) with eltype Int64:
 1  3  4

Or multiply the vector by an scalar:

4x
3-element Vector{Int64}:
  4
 12
 16

In cases where the operator is not clear, we need to use the dot operator to make element-by-element computations:

1 .+ x
3-element Vector{Int64}:
 2
 4
 5
sqrt.(x)
3-element Vector{Float64}:
 1.0
 1.7320508075688772
 2.0

The dot operator is atuomatically available for any function:

g(x) = 3 + 2x^2
g (generic function with 1 method)
g.(x)
3-element Vector{Int64}:
  5
 21
 35

Matrices

A = [1 4 5;
    3 4 5]
2×3 Matrix{Int64}:
 1  4  5
 3  4  5
5A
2×3 Matrix{Int64}:
  5  20  25
 15  20  25
5 .+ A
2×3 Matrix{Int64}:
 6  9  10
 8  9  10
A * x
2-element Vector{Int64}:
 33
 35
A .^ 2
2×3 Matrix{Int64}:
 1  16  25
 9  16  25
g.(A)
2×3 Matrix{Int64}:
  5  35  53
 21  35  53

Constructors

zeros(5, 3)
5×3 Matrix{Float64}:
 0.0  0.0  0.0
 0.0  0.0  0.0
 0.0  0.0  0.0
 0.0  0.0  0.0
 0.0  0.0  0.0
ones(5, 3)
5×3 Matrix{Float64}:
 1.0  1.0  1.0
 1.0  1.0  1.0
 1.0  1.0  1.0
 1.0  1.0  1.0
 1.0  1.0  1.0
rand(5, 3)
5×3 Matrix{Float64}:
 0.498434  0.184079  0.258119
 0.389721  0.46979   0.888894
 0.26454   0.568002  0.723021
 0.719424  0.677608  0.51986
 0.676602  0.105514  0.0459551
randn(5, 3)
5×3 Matrix{Float64}:
 -0.253313   0.910287   1.6276
  1.90599   -0.180697  -0.474504
  1.0981    -0.557519   0.713292
 -0.490607   0.37708   -0.554788
 -1.63391    0.559958  -0.760346
ones(5, 5) + I
5×5 Matrix{Float64}:
 2.0  1.0  1.0  1.0  1.0
 1.0  2.0  1.0  1.0  1.0
 1.0  1.0  2.0  1.0  1.0
 1.0  1.0  1.0  2.0  1.0
 1.0  1.0  1.0  1.0  2.0