Arrays¶

An array is an n-dimensional matrix. The elements are accessed by "row" and "column" indices.

Vectors¶

A Vector is just what you think it is: a 1-dimensional array. In Julia, these are always column vectors (sort of always).

To create a vector, simply fill it with values:

a = [1, 2, 3]
3-element Array{Int64,1}:
1
2
3


Note that the return values is a one-dimensional array. Vector is simply an alias for this.

julia> Vector{Int} === Array{Int,1}
true


There are no row vectors in Julia. A row vector is a 1xN Matrix:

julia> a = [1 2 3]
1×3 Array{Int64,2}:
1  2  3

julia> a isa Matrix{Int}
true


Indexing¶

To extract elements from a vector, hand it a list of indices.

julia> a = [2, 3, 4, 5];
julia> a[1]
2
# Note the double brackets
julia> a[[1, 3]]
2-element Array{Int64,1}:
2
4
# What we are really doing is
idx = [1, 3];
a[idx]


Then there is logical indexing:

a = [1, 2, 3];
a[[true, false, true]]
2-element Array{Int64,1}:
1
3


Arrays are "mutable", meaning that the elements can be changed "in-place":

julia> a = [1,2,3]
3-element Vector{Int64}:
1
2
3

julia> a[2] = 22
22

julia> a
3-element Vector{Int64}:
1
22
3


Ranges¶

In Matlab, 1:3 is a Vector, but not in Julia:

julia> 1:3
1:3

julia> typeof(1:3)
UnitRange{Int64}


The main difference between a Vector and a Range is that the vector is allocated upon construction (its values are computed and stored in memory).

A Range is an Iterator. You can iteratore over its elements, but they are not computed until needed. Therefore, the Range does not allocate memory. This is a big deal when lots of values are involved.

However, a Range is an AbstractArray; so all the indexing that works on Vectors also works on Ranges.

collect makes a Range into a Vector.

Exercises¶

Start with x = 1 : 3 : 30.

1. Find all even elements.
2. Find all elements between 5 and 20.
3. Set all even elements to their negative values.

Matrices¶

A Matrix is a 2-dimensional array. There are also n-dimensional arrays (see below).

To create a matrix, simply fill it with values.

julia> a = [1 2 3; 4 5 6]
2×3 Array{Int64,2}:
1  2  3
4  5  6


Many commands work directly on matrices.

julia> a = [1 2 3]; b = [2; 1; 1]; a * b
1-element Array{Int64,1}:
7
julia> c=b*a
3×3 Array{Int64,2}:
2  4  6
1  2  3
1  2  3


To extract elements:

julia> c[1,2]
4

julia> c[1:2, 2:3]
2×2 Array{Int64,2}:
4  6
2  3


To extract a row (but note that this produces a Vector):

julia> c[1,:]
3-element Array{Int64,1}:
2
4
6


Linear indexing works as well:

julia> c[4]
4
julia> c[4] == c[1,2]
true


In memory, Arrays are stored like Vectors. The compiler just keeps track of how the user wants to interpret the data.

Multi-dimensional Arrays¶

Arrays can have more than 2 dimensions.

julia> using Random

julia> a = rand(4,3,2)
4×3×2 Array{Float64,3}:
[:, :, 1] =
0.146411  0.908415  0.568039
0.444156  0.434034  0.875361
0.429705  0.704086  0.71789
0.502228  0.124412  0.771808

[:, :, 2] =
0.862953   0.58189    0.549077
0.722745   0.677342   0.330188
0.0543144  0.161877   0.358381
0.312414   0.0681076  0.822319


Indexing generates new arrays with potentially fewer dimensions:

julia> a[:,1,:]
4×2 Array{Float64,2}:
0.146411  0.862953
0.444156  0.722745
0.429705  0.0543144
0.502228  0.312414


Array Exercises¶

1. Construct a matrix A with elements [2,4,...,20] in row 1 and [1,4,7,...,28] in row 2.
2. Replace row 1 with its square.
3. Find all columns where row 1 > row 2.
4. Let x=ones(10,1). Compute Ax.