Introduction to arrays

Introduction to Julia

James Fulton

Climate informatics researcher

What is an array?

# Store run times with many variables
runtime1 = 33.1
runtime2 = 32.7
runtime3 = 34.2
runtime4 = 31.9
# Store runtimes in array
runtimes = [33.1, 32.7, 34.2, 31.9]
  • List of values
  • Surrounded by [ ]
  • With , in beween values
Introduction to Julia

Arrays vs. vectors vs. matrices

# Store runtimes in array
runtimes = [33.1, 32.7, 34.2, 31.9]

println(typeof(runtimes))
Vector{Float64}
Introduction to Julia

Arrays vs. vectors vs. matrices

The image shows a vector as a one-dimensional line of values.

Introduction to Julia

Arrays vs. vectors vs. matrices

The image shows a matrix as a two-dimensional grid of values.

Introduction to Julia

Arrays vs. vectors vs. matrices

The image shows an array as a many-dimensional grid of values.

Introduction to Julia

Array data types

# Store runtimes in array
runtimes = [33.1, 32.7, 34.2, 31.9]

println(typeof(runtimes))
Vector{Float64}
Introduction to Julia

Array data types

# Store runtimes in array
runtimes = [33.1, 32.7, 34.2, 31.9]

println(eltype(runtimes))
Float64
Introduction to Julia

Array data types

# Store integers in array
number_of_customers = [11, 19, 31, 27]

println(typeof(number_of_customers))
Vector{Int64}
# Store strings in array
names = ["Amit", "Barbara", "Carlos"]

println(typeof(names))
Vector{String}
# Store characters in array
grades = ['A', 'B', 'B', 'A']

println(typeof(grades))
Vector{Char}
# Store booleans in array
correct_answers = [true, false, true]

println(typeof(correct_answers))
Vector{Bool}
Introduction to Julia

Mixed data types

# Store multiple types in array
#        string   bool  int char float
items = ["James", true, 10, 'B', -20.3]

println(typeof(items))
Vector{Any}
Introduction to Julia

Mixed data types

# Store array of item names
item_names = ["chalk", "cheese", "eggs", "ham"]

# Store array of item prices
item_prices = [2.30, 3.50, 4.25, 2.00]

println(typeof(item_names))
println(typeof(item_prices))
Vector{String}
Vector{Float64}
Introduction to Julia

Indexing arrays

# Index:         1        2        3       4
item_names = ["chalk", "cheese", "eggs", "ham"]

# Index:         1     2     3     4
item_prices = [2.30, 3.50, 4.25, 2.00]

println(item_names[1])
println(item_prices[1])
chalk
2.30
Introduction to Julia

Indexing arrays

# Index:         1        2        3       4
item_names = ["chalk", "cheese", "eggs", "ham"]

# Index:         1     2     3     4
item_prices = [2.30, 3.50, 4.25, 2.00]

println(item_names[end])
println(item_prices[end])
ham
2.00
Introduction to Julia

Indexing arrays

# Index:         1        2        3       4
item_names = ["chalk", "cheese", "eggs", "ham"]

# Index:         1     2     3     4
item_prices = [2.30, 3.50, 4.25, 2.00]

println(item_names[end-1])
println(item_prices[end-1])
eggs
4.25
Introduction to Julia

Slicing arrays

# Index:         1        2        3       4
item_names = ["chalk", "cheese", "eggs", "ham"]

# Index:         1     2     3     4
item_prices = [2.30, 3.50, 4.25, 2.00]

println(item_names[1:2])
["chalk", "cheese"]
Introduction to Julia

Let's practice!

Introduction to Julia

Preparing Video For Download...