Julia_Arrays
Julia_Arrays
Arrays
• An Array is an ordered set of elements which are often specified with squared brackets having
comma-separated items. We can create arrays that are:
• Full or empty
• Hold values of different types
• Restricted to values of a specific type
• In Julia, arrays are actually mutable type collections which are used for lists, vectors, tables,
and matrices. That is why the values of arrays in Julia can be modified with the use of certain pre-
defined keywords. With the help of push! command you can add new element in array. Similarly,
with the help of splice! function you can add elements in an array at a specified index.
• CreatingSimple1DArrays
• Following is the example showing how we can create a simple 1D array:
julia> arr = [1,2,3]
3-element Array{Int64,1}:
1
2
3
The above example shows that we have created a 1D array with 3 elements each of which
is a 64-bit integer. This 1D array is bound to the variable arr.
Uninitialized array
We can also specify the type and the dimension of an array by using the below syntax:
Array{type}(dims)
Following is an example of uninitialized array:
julia> array = Array{Int64}(undef, 3)
3-element Array{Int64,1}:
0
0
0
julia> array = Array{Int64}(undef, 3, 3, 3)
3×3×3 Array{Int64,3}:
[:, :, 1] =
8 372354944 328904752
3 331059280 162819664
32 339708912 1
[:, :, 2] =
331213072 3 331355760
1 328841776 331355984
-1 328841680 2
[:, :, 3] =
1 0 339709232
Empty Arrays
Just like creating an array of specific type, we can also create empty arrays in Julia. The
example is given below:
julia> A = Int64[]
Int64[]
julia> A = String[]
String[]
Creating2Darrays& matrices
Leave out the comma between elements and you will be getting 2D arrays in Julia. Below
is the example given for single row, multi-column array:
julia> [1 2 3 4 5 6 7 8 9 10]
1×10 Array{Int64,2}:
1 2 3 4 5 6 7 8 9 10
To add another row, just add a semicolon(;). Let us check the below example:
julia> [1 2 3 4 5 ; 6 7 8 9 10]
2×5 Array{Int64,2}:
1 2 3 4 5
6 7 8 9 10
Creatingarraysusingrangeobjects
We can create arrays using range objects in the following ways:
Collect() function
First useful function to create an array using range objects is collect(). With the help of
colon(:) and collect() function, we can create an array using range objects as follows:
julia> collect(1:5)
5-element Array{Int64,1}:
1
2
3
4
5
We can also create arrays with floating point range objects:
julia> collect(1.5:5.5)
5-element Array{Float64,1}:
1.5
julia> collect(0:5:50)
11-element Array{Int64,1}:
0
5
10
……
Array{Int64,2}:
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
6 12 18 24 30 36 42 48 54 60
7 14 21 28 35 42 49 56 63 70
8 16 24 32 40 48 56 64 72 80
9 18 27 36 45 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100
Similar to comprehension, we can use generator expressions to create an array:
Array{Int64,1}:
1
4
9
PopulatinganArray
zeros (m, n)
This function will create matrix of zeros with m number of rows and n number of columns.
The example is given below:
0.0 0.0 0.0 0.0 0.0
julia> zeros(4,5)
0.0 0.0 0.0 0.0 0.0
4×5 Array{Float64,2}:
0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0
ones (m, n)
This function will create matrix of ones with m number of rows and n number of columns.
The example is given below:
julia> ones(4,5)
4×5 Array{Float64,2}:
fill()
This function is used to fill an array with a specific value. More specifically, it will create
an array of repeating duplicate value.
julia> fill(100,5)
5-element Array{Int64,1}:
100
100
100
100
100
fill!()
It is similar to fill() function but the sign of exclamation (!) is an indication or warning that
the content of an existing array is going to be changed. The example is given below:
julia> fill!(ABC,100)
5-element Array{Float64,1}:
100.0
100.0
100.0
100.0
100.0
ArrayConstructor
The function Array(), we have studied earlier, can build array of a specific type as follows:
julia> Array{Int64}(undef, 3)
5-element Array{Int64,1}:
4294967297
8589934593
8589934594
Arraysof arrays It can also be created with the help of Array constructor as follows:
julia> ABC =
julia> Array[1:5,6:10]
Array[[3,4],[5,6]]
2-element
2-element
Array{Array,1}:
Array{Array,1}:
[1, 2, 3, 4, 5]
[3, 4]
[5, 6] [6, 7, 8, 9, 10]
Copying arrays
Suppose you have an array and want to create another array with similar dimensions,
then you can use similar() function as follows:
julia> A = collect(1:5);
Here we have hide the values with the help of semicolon(;)
julia> B = similar(A)
5-element Array{Int64,1}:
164998448
234899984 Here the dimension of array A are copied but not values.
383606096
MatrixOperations
As we know that a two-dimensional (2D) array can be used as a matrix so all the functions that are available for
working on arrays can also be used as matrices. The condition is that
the dimensions and contents should permit. If you want to type a matrix, use spaces to make rows and
semicolon(;) to separate the rows as follows:
Following is an example to create an array of arrays (as we did earlier) by
julia> [2 3 ; 4 5] placing two arrays next to each other:
julia> arr[[2,5,6]]
3-element Array{Int64,1}: To access more than one element at a time, we can
also provide a bunch of index numbers as shown
10 below:
25
30
13 14 15
16 17 18
julia> arr2[1,2] To access row1, column2 element, we need to use the
command below:
11
AddingElements
We can add elements to an array in Julia at the end, at the front and at the given index
using push!(), pushfirst!() and splice!() functions respectively.
At the end
We can use push!() function to add an element at the end of an array. For example,
julia> push!(arr,55)
11-element Array{Int64,1}:
Remember we had 10 elements in array arr. Now push!() function added the element 55
at the end of this array.
The exclamation(!) sign represents that the function is going to change the array.
At the front
We can use pushfirst!() function to add an element at the front of an array. For example,
julia> pushfirst!(arr,0)
12-element Array{Int64,1}:
At a given index
We can use splice!() function to add an element into an array at a given index. For
example,
julia> splice!(arr,2:5,2:6)
4-element Array{Int64,1}:
RemovingElements
We can remove elements at last position, first position and at the given index, from an
array in Julia, using pop!(), popfirst!() and splice!() functions respectively.
julia> popfirst!(arr)
0