4  Arrays

Julia includes builtin N-dimensional arrays.

Build an Array using square brackets:

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

4.1 Initialize Array

There are multiple ways to initialize an Array. The main constructor Array can be used with the syntax Array{T}(undef, dims) for T of a given type. For example:

af = Array{Float64}(undef, 10, 4)
10×4 Matrix{Float64}:
 0.0       5.0e-324  0.0  0.0
 5.0e-324  0.0       0.0  0.0
 5.0e-324  0.0       0.0  0.0
 0.0       5.0e-324  0.0  0.0
 0.0       5.0e-324  0.0  0.0
 5.0e-324  5.0e-324  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
 5.0e-324  0.0       0.0  0.0
ai = Array{Int64}(undef, 10, 4)
10×4 Matrix{Int64}:
 4500111984  4501967728  5225787808  5293716816
          0           0           0           1
 5225779120  4501086640  5293716816  5293716816
          0           1           1           1
 4500112000  5153490560  5293716816  5293716816
          0           0           1           1
 5153520176  5208729952  5293716816  5293716816
          0           0           1           1
 5208757600  4522071440  5293716816  5153520176
          0           0           1           0
as = ["a", "b", "c", "d", "e"]
5-element Vector{String}:
 "a"
 "b"
 "c"
 "d"
 "e"

#You can define the number of dimensions using Array{Float64, 2}(undef, 10, 4), but it is inferred from the length of integers after undef

4.1.1 1D Array

a = [1, 2, 3, 4, 5]
5-element Vector{Int64}:
 1
 2
 3
 4
 5

Create array with defined type

a = Array{Float64}([1, 2, 3, 4, 5])
5-element Vector{Float64}:
 1.0
 2.0
 3.0
 4.0
 5.0

Create array using a range and collect():

b = 11:15
11:15
typeof(b)
UnitRange{Int64}
b = collect(11:15)
5-element Vector{Int64}:
 11
 12
 13
 14
 15

4.2 Convenient functions to build Arrays:

fill(42.0, 3)
3-element Vector{Float64}:
 42.0
 42.0
 42.0
repeat([1, 2, 3], 2)
6-element Vector{Int64}:
 1
 2
 3
 1
 2
 3
ones(3)
3-element Vector{Float64}:
 1.0
 1.0
 1.0
zeros(3)
3-element Vector{Float64}:
 0.0
 0.0
 0.0
trues(3)
3-element BitVector:
 1
 1
 1
falses(3)
3-element BitVector:
 0
 0
 0

4.2.1 ND Array

Use ; to separate rows:

d = [1 2 3; 4 5 6]
2×3 Matrix{Int64}:
 1  2  3
 4  5  6
x = collect(1:12)
12-element Vector{Int64}:
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
xm = reshape(x, 4, 3)
4×3 Matrix{Int64}:
 1  5   9
 2  6  10
 3  7  11
 4  8  12

To fill row-wise, we can use permutedims() (and switch n rows <-> ncols)

permutedims(reshape(x, 3, 4))
4×3 Matrix{Int64}:
  1   2   3
  4   5   6
  7   8   9
 10  11  12

You can use : if you hate multiplication / division:

reshape(x, 4, :)
4×3 Matrix{Int64}:
 1  5   9
 2  6  10
 3  7  11
 4  8  12
xt = reshape(x, 3, 2, 2)
3×2×2 Array{Int64, 3}:
[:, :, 1] =
 1  4
 2  5
 3  6

[:, :, 2] =
 7  10
 8  11
 9  12

ones(), zeros(), trues(), falses() can all construct ND Arrays:

ones(2, 3, 2)
2×3×2 Array{Float64, 3}:
[:, :, 1] =
 1.0  1.0  1.0
 1.0  1.0  1.0

[:, :, 2] =
 1.0  1.0  1.0
 1.0  1.0  1.0

4.2.2 Concatenation

1-D Array of 1-D Arrays:

c = ["a", "b", "c", "d", "e"]
5-element Vector{String}:
 "a"
 "b"
 "c"
 "d"
 "e"
arr = [a, b, c]
3-element Vector{Vector}:
 [1.0, 2.0, 3.0, 4.0, 5.0]
 [11, 12, 13, 14, 15]
 ["a", "b", "c", "d", "e"]

Horizontal concatenation: 2-D Array

arr = hcat(a, b, c)
5×3 Matrix{Any}:
 1.0  11  "a"
 2.0  12  "b"
 3.0  13  "c"
 4.0  14  "d"
 5.0  15  "e"

Vertical concatenation: Combine 1-D Arrays into a a longer 1-D Array

v = [a; b]
10-element Vector{Float64}:
  1.0
  2.0
  3.0
  4.0
  5.0
 11.0
 12.0
 13.0
 14.0
 15.0

or using vcat

v = vcat(a, b)
10-element Vector{Float64}:
  1.0
  2.0
  3.0
  4.0
  5.0
 11.0
 12.0
 13.0
 14.0
 15.0

4.3 List defined variables

varinfo()
name size summary
BUFFER_SIZE 8 bytes Int64
Base Module
Core Module
MSG_BOUNDARY 50 bytes 10-element Vector{UInt8}
Main Module
MsgID 124 bytes DataType
MsgType 7 bytes @NamedTuple{from_host_call_with_response::UInt8, from_host_call_without_response::UInt8, from_host_fake_interrupt::UInt8, from_worker_call_result::UInt8, from_worker_call_failure::UInt8, special_serialization_failure::UInt8, special_worker_terminated::UInt8}
Notebook 1.760 KiB Module
_buffer_writes 0 bytes _buffer_writes (generic function with 1 method)
_channel_cache 456 bytes Dict{UInt64, AbstractChannel} with 0 entries
_discard_until_boundary 0 bytes _discard_until_boundary (generic function with 1 method)
_serialize_msg 0 bytes _serialize_msg (generic function with 1 method)
format_error 0 bytes format_error (generic function with 1 method)
handle 0 bytes handle (generic function with 3 methods)
interrupt 0 bytes interrupt (generic function with 2 methods)
main 0 bytes main (generic function with 1 method)
refresh! 0 bytes refresh! (generic function with 1 method)
render 0 bytes render (generic function with 1 method)
serve 0 bytes serve (generic function with 1 method)

4.4 Set operations

a = [1, 3, 5, 9];
b = [2, 3, 4, 9, 11];

Is 3 in b?

in(b)(3)
true

For each element of a, check if it exists in b

in(b).(a)
4-element BitVector:
 0
 1
 0
 1
in(a).(b)
5-element BitVector:
 0
 1
 0
 1
 0

4.5 Indexing

Let’s look at logical and integer indexing.
Indexing in Julia is 1-based.

x = collect(15:24)
10-element Vector{Int64}:
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24

Get the 5th element of a vector (integer index):

x[5]
19

Get elements 6 through 9 of the same vector (integer index):

x[6:9]
4-element Vector{Int64}:
 20
 21
 22
 23

Select elements with value greater than 19 (logical index):

x[x .> 19]
5-element Vector{Int64}:
 20
 21
 22
 23
 24

4.6 Logical Index

x = collect(21:30)
10-element Vector{Int64}:
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30

Create a logical index using two inequalities.
Notice how you need to use parentheses for the elementwise .& to work in this case:

idl = (x .> 23) .& (x .< 28)
10-element BitVector:
 0
 0
 0
 1
 1
 1
 1
 0
 0
 0

4.7 Logical to integer indexing with findall()

idi = findall(idl)
4-element Vector{Int64}:
 4
 5
 6
 7

4.8 Integer to logical index

Initialize a BitArray:

idl2 = BitArray(undef, length(x))
10-element BitVector:
 0
 0
 0
 0
 0
 0
 0
 0
 0
 0

or using falses():

idl2 = falses(length(x))
10-element BitVector:
 0
 0
 0
 0
 0
 0
 0
 0
 0
 0

Use the integer index to replace the corresponding elements:

idl2[idi] .= true
4-element view(::BitVector, [4, 5, 6, 7]) with eltype Bool:
 1
 1
 1
 1
idl2
10-element BitVector:
 0
 0
 0
 1
 1
 1
 1
 0
 0
 0

4.9 Exclude elements using an index

Negate a logical index with ! / .!

x[.!idl]
6-element Vector{Int64}:
 21
 22
 23
 28
 29
 30
x[idi]
4-element Vector{Int64}:
 24
 25
 26
 27