Common Data Structures

Arrays

Array’s are N-dimensional data structures of a common type (the type could be Any). The type signature is

Array{T, N}

where T is the type and N is the dimension. You can omit N and it will be inferred for the arguments

Array{Any, 3}(nothing, 3, 2, 3)
3×2×3 Array{Any, 3}:
[:, :, 1] =
 nothing  nothing
 nothing  nothing
 nothing  nothing

[:, :, 2] =
 nothing  nothing
 nothing  nothing
 nothing  nothing

[:, :, 3] =
 nothing  nothing
 nothing  nothing
 nothing  nothing
x = Array{Any}(nothing, 2, 3)
2×3 Matrix{Any}:
 nothing  nothing  nothing
 nothing  nothing  nothing

A Vector is an Array with N=1, a Matrix is an Array with N=2.

Vector{Float64}(undef, 3)
3-element Vector{Float64}:
 0.0
 6.91051096480046e-310
 6.91054319031837e-310
Matrix{Float64}(undef, 3, 3)
3×3 Matrix{Float64}:
 6.4e-323  3.0e-323  1.0e-323
 3.5e-323  2.0e-323  5.0e-324
 2.5e-323  1.5e-323  6.91054e-310
Float64[1.0, 2.0, 3.0]
3-element Vector{Float64}:
 1.0
 2.0
 3.0
y = Float64[1.0 2.0 3.0; 4.0 5.0 6.0]
2×3 Matrix{Float64}:
 1.0  2.0  3.0
 4.0  5.0  6.0

Array indexing and assignment

y[1, 2]
2.0
y[1:2, 2]
2-element Vector{Float64}:
 2.0
 5.0
y[end,end] = 100;
y
2×3 Matrix{Float64}:
 1.0  2.0    3.0
 4.0  5.0  100.0
x[1, 1] = "test"
x[2, 1:3] .= 1
x
2×3 Matrix{Any}:
  "test"   nothing   nothing
 1        1         1

Views

z = y[1:2, 1:2]
2×2 Matrix{Float64}:
 1.0  2.0
 4.0  5.0
z[1,1] = 100.0
@show z
@show y;
z = [100.0 2.0; 4.0 5.0]
y = [1.0 2.0 3.0; 4.0 5.0 100.0]
z = @view y[1:2, 1:2]
z[1, 1] = 100.0
y
2×3 Matrix{Float64}:
 100.0  2.0    3.0
   4.0  5.0  100.0

Convenience constructors

zeros(Int32, 2, 3)
2×3 Matrix{Int32}:
 0  0  0
 0  0  0
zeros(2, 3)
2×3 Matrix{Float64}:
 0.0  0.0  0.0
 0.0  0.0  0.0
ones(2, 3)
2×3 Matrix{Float64}:
 1.0  1.0  1.0
 1.0  1.0  1.0
fill(77.0, 2, 2)
2×2 Matrix{Float64}:
 77.0  77.0
 77.0  77.0
fill!(y, 22.0)
2×3 Matrix{Float64}:
 22.0  22.0  22.0
 22.0  22.0  22.0

Operations on arrays

Multiplication of a Matrix and a Vector automatically does matrix multiplication.

@show y
@show x = [1.0, 2.0, 3.0]
y * x
y = [22.0 22.0 22.0; 22.0 22.0 22.0]
x = [1.0, 2.0, 3.0] = [1.0, 2.0, 3.0]
2-element Vector{Float64}:
 132.0
 132.0

If we want element-wise multiplication we need to use .*.

x .* x
3-element Vector{Float64}:
 1.0
 4.0
 9.0

Addition/subtraction of two Arrays of the same size gives element-wise addition.

x + x
3-element Vector{Float64}:
 2.0
 4.0
 6.0

We can broadcast the addition operation along matching dimensions of Arrays.

@show size(y')
@show size(x)

y' .+ x
size(y') = (3, 2)
size(x) = (3,)
3×2 Matrix{Float64}:
 23.0  23.0
 24.0  24.0
 25.0  25.0

Any scalar operation can be vectorized with the . operator, i.e.

my_scalar_operation(x, y) = x * y + x^2 * y^2

my_scalar_operation(1.0, 2.0)
6.0
my_scalar_operation.([1.0, 2.0, 3.0], [4.0, 5.0, 6.0])
3-element Vector{Float64}:
  20.0
 110.0
 342.0

Array functions

There are numerous array functions such as sum, cumsum, prod, etc. For example

accumulate(+, [1, 2, 3])
3-element Vector{Int64}:
 1
 3
 6

See Array documentation for more examples.

Tuples

Tuples are fixed length containers that can hold any values, but cannot be modified (they are immutable).

x = ("a string", 1.0, 33)
("a string", 1.0, 33)
x[1]
"a string"
x[2] = 2.0 
MethodError: no method matching setindex!(::Tuple{String, Float64, Int64}, ::Float64, ::Int64)

Stacktrace:
 [1] top-level scope
   @ In[28]:1
 [2] eval
   @ ./boot.jl:373 [inlined]
 [3] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)
   @ Base ./loading.jl:1196

Named Tuples

x = (a = "a string", b = 1.0, c = 33) 
(a = "a string", b = 1.0, c = 33)
@show x[1]
@show x.a;
x[1] = "a string"
x.a = "a string"
_, bb, _ = x
@show bb;
bb = 1.0

Dictionaries

Dictionaries are data structures to store and lookup key-value pairs, i.e.

dict = Dict("a" => "a string", "b" => 33, "name" => "John")

@show dict["b"]
@show dict["name"];
dict["b"] = 33
dict["name"] = "John"

They can also be constructed using a collection of tuples.

dict = Dict([("a","a string"), ("b", 33), ("name", "John")])

@show dict["b"]
@show dict["name"];
dict["b"] = 33
dict["name"] = "John"

More Data Structures

More information of these data structures as well as many others that Julia offers can be found in the documentation here