Operating on arrays

Introduction to Julia

James Fulton

Climate informatics researcher

Basic array functions

# An array with 6 elements
x = ["a", "b", "sea", "d", "e", "f"]


# Find the length of the array l = length(x)
println(l)
6
Introduction to Julia

Basic array functions

# An array with 6 elements
x = ["a", "b", "sea", "d", "e", "f"]

x_sorted = sort(x)
println(x_sorted)
["a", "b", "d", "e", "f", "sea"]
# An array with 6 elements
x = [100, 95, 9, 22, 75, 58]

x_sorted = sort(x)
println(x_sorted)
[9, 22, 58, 75, 95, 100]
Introduction to Julia

Vectorized operations

# Gradient and intercept
m = -0.32
c = 34.8

# Next run number is 16 x = 16
# Predict next run time y = m * x + c
println(y)
29.68

A plot of run times vs run number shows a downwards trend the time taken to complete each run.

Introduction to Julia

Vectorized operations

# Gradient and intercept
m = -0.32
c = 34.8

# Next run numbers
x = [16, 17, 18, 19, 20]

# Predict next run time y = m * x + c
ERROR: MethodError: ...

A plot of run times vs run number shows a downwards trend the time taken to complete each run.

Introduction to Julia

Array addition

Add scalar

a = [1,2,3]


# Answer we expect is [3,4,5]
println(a .+ 2)
[3, 4, 5]

Add array

a = [1,2,3]
b = [1,2,3]

# Answer we expect is [2,4,6]
println(a .+ b)
[2, 4, 6]
Introduction to Julia

Array addition

Add scalar

a = [1,2,3]


# Answer we expect is [3,4,5]

println(a + 2)
ERROR: MethodError: ...

Add array

a = [1,2,3]
b = [1,2,3]

# Answer we expect is [2,4,6]

println(a + b)
[2, 4, 6]
Introduction to Julia

Array subtraction

# Subtract scalar
println(a .- 1)
[0, 1, 2]
# Subtract array
println(a .- b)
[0, 0, 0]
# Subtract scalar
println(a - 1)
ERROR: MethodError: ...
# Subtract array
println(a - b)
[0, 0, 0]
Introduction to Julia

Array multiplication

Multiply by scalar

a = [1,2,3]


# Answer we expect is [5,10,15]
println(a .* 5)
[5, 10, 15]

Multiply by array

a = [1,2,3]
b = [1,2,3]

# Answer we expect is [1,4,9]
println(a .* b)
[1, 4, 9]
Introduction to Julia

Array multiplication

Multiply by scalar

a = [1,2,3]


# Answer we expect is [5,10,15]
println(a * 5)
[5, 10, 15]

Multiply by array

a = [1,2,3]
b = [1,2,3]

# Answer we expect is [1,4,9]
println(a * b)
ERROR: MethodError: ...
Introduction to Julia

Array division

# Divide by scalar
println(a ./ 2)
[0.5, 1.0, 1.5]
# Divide by array
println(a ./ b)
[1.0, 1.0, 1.0]
# Divide by scalar
println(a / 2)
[0.5, 1.0, 1.5]
# Divide by array
println(a / b)
 0.071   0.142  0.214
 0.142   0.285  0.428
 0.214   0.428  0.642
Introduction to Julia

Vectorized operations

# Gradient and intercept
m = -0.32
c = 34.8

# Next run numbers
x = [16, 17, 18, 19, 20]

# Predict next run time
y = m * x + c


A plot of run times vs run number shows a downwards trend the time taken to complete each run.

Introduction to Julia

Vectorized operations

# Gradient and intercept
m = -0.32
c = 34.8

# Next run numbers
x = [16, 17, 18, 19, 20]

# Predict next run time
y = m .* x .+ c


println(y)
[29.68, 29.36, 29.04, 28.72, 28.40]

A plot of run times vs run number shows a downwards trend the time taken to complete each run.

Introduction to Julia

Cheatsheet

For arrays a and b

Operation Scalar example Array example
Addition a .+ 1 a .+ b or a + b
Subtraction a .- 1 a .- b or a - b
Multiplication 2 .* a or 2 * a a .* b
Division a ./ 2 or a / 2 a ./ b
Introduction to Julia

Let's practice!

Introduction to Julia

Preparing Video For Download...