Arrays and Matrices#
Learning Objectives#
Understand the concept of arrays and matrices in Julia
Create and initialise one-dimensional and multi-dimensional arrays
Utilize various array creation functions in Julia
Perform basic manipulations such as indexing and slicing on arrays
Implement common operations on arrays and matrices
Arrays#
Arrays are a fundamental data structure within Julia, used to store collection of elements.
One-Dimensional Arrays#
One-dimensional arrays, also known as vector, can be created sing square brackets, as seen below.
Declare and Initialise#
a = [1,2,3]
println(a)
[1, 2, 3, 4, 5]
Constructor#
It is also possible to make use of the Array
constructor, allowing for more control over the arrays properties, relevant to when considering specific performance elements. The use of the constructor can be helpful in situations where performance is needed as to allow for the array to remain uninitialised until the final values are put into the array, and not requiring the array to be populated with zeros and ones until that time arrives.
The code below creates an uninitialized arrays of integers with 5 elements. If you print the values for the array you will get a different
b = Array{Int}(undef, 3)
println(b)
[5288981504, 5288981504, 5288981504, 5288981504, 0]
Ranges and Comprehension#
It is also possible to create arrays with the use of ranges and comprehension as seen below. If the value of c
is printed as it is, it will give a UnitRange
object, the collect
function is able to convert the range into an array, which then gives the format seen before when printing the variable.
c = 1:3 #Range
println(c)
println(collect(c))
d = [i for i in 1:3] #Comprehension
println(d)
1:3
[1, 2, 3]
[1, 2, 3]
Common Array Creation Functions#
There are a number of different functions within Julia that are able to create arrays that are based on common formats such as:
Zeros:
zeros()
creates a matrix of zeros based on the dimensions specified.Ones:
ones()
creates a matrix of ones based on the dimensions specified.Random [0,1]:
rand()
creates a matrix of random numbers between 0 and 1 based on the dimensions specified.Random Normally Distributed:
randn()
creates a matrix of random numbers that are normally distributed based on the dimensions specified.
zeros_a = zeros(3)
println("Array of zeroes: ", zeros_a)
ones_a = ones(3)
println("Array of ones: ", ones_a)
rand_a = rand(3)
println("Array of random values between 0 and 1: ", rand_a)
randn_a = randn(3)
println("Array of random values normally distributed: :", randn_a)
Array of zeroes: [0.0, 0.0, 0.0]
Array of ones: [1.0, 1.0, 1.0]
Array of random values between 0 and 1: [0.9211204080959933, 0.21859837290202477, 0.6479622631097238]
Array of random values normally distributed: :[-1.5830105669798484, 0.34365184518873737, 1.1434737668472543]
Multi-Dimensional Arrays#
Multi-dimensional arrays can be created in a similar manner as vector, with the syntax as follows to create a three by three matrix.
b = [1 2 3; 4 5 6; 7 8 9]
println(b)
[1 2 3; 4 5 6; 7 8 9]
All of the previous methods for creating a one-dimensional array can be used to create a two-dimensional array, such as below.
b = Array{Int}(undef, 3, 3 )
println(b)
[13068695152 5328927200 13068694512; 4298229872 13068695312 5330123136; 4298227720 4298231024 0]
zeros_a = zeros(3,3)
println("Array of zeroes: ", zeros_a)
ones_a = ones(3,3)
println("Array of ones: ", ones_a)
rand_a = rand(3,3)
println("Array of random values between 0 and 1: ", rand_a)
randn_a = randn(3,3)
println("Array of random values normally distributed: :", randn_a)
Array of zeroes: [0.0 0.0 0.0; 0.0 0.0 0.0; 0.0 0.0 0.0]
Array of ones: [1.0 1.0 1.0; 1.0 1.0 1.0; 1.0 1.0 1.0]
Array of random values between 0 and 1: [0.4344901162658211 0.12016405686125153 0.5163361722350794; 0.27309606604908243 0.33707453186083436 0.12897967820773593; 0.3638697535983929 0.33234882476239824 0.9725342484111058]
Array of random values normally distributed: :[0.3599868335039648 -1.7324983872796258 0.5335426859739404; 0.9597702701198646 1.3589689913664549 0.8503624020431969; -1.764525511350084 -0.5981769209816526 -0.2306590229156192]
Manipulation of Arrays#
Manipulating arrays involved accessing and modifying their elements, as well as performing various operations on them.
Indexing#
Square brackets with indices can be used to access specific elements. Of note is that in Julia indexing starts at 1.
a = [10,20,30,40,50]
println(a[3]) # Indexing for one dimensional arrays
b = [10 20 30 40 50;60 70 80 90 100]
println(b[2,3])
30
80