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]

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 unitiatilised 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)
[5252985856, 5252985856, 5252985856]

Rnages 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.36161088254446083, 0.5271275976452546, 0.4349954138988883]
Array of random values normally distributed: :[1.581302355605327, 0.9386730069509243, 0.4770797245798432]

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)
[4514514112 0 5323897968; 0 5251637568 0; 5307340480 0 4482587216]
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.018999269290326692 0.6457723682301038 0.9526789191484026; 0.6223666594334827 0.8329564034868431 0.03195515501109136; 0.02309329141358507 0.06125396810056627 0.5740250652296306]
Array of random values normally distributed: :[0.2131908446647018 0.08234447083219733 -1.3353116382450538; -0.6799997136741219 -0.7884857971729337 0.4104871518079462; 1.0491054813517036 -1.566600926570642 -1.7913183902800496]

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 indexesing 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