Tutorial: Vectors in Julia
Vectors in Julia differ a bit from Matlab. In Matlab, everything is an array, including vectors (and even scalars). In Julia, there are distinct data types for scalars, vectors, rowvectors, and 1D arrays. This tutorial illustrates the differences.
- Jeff Fessler, University of Michigan
- 2017-07-24, original
- 2020-08-05, Julia 1.5.0
- 2021-08-23, Julia 1.6.2
- 2023-08-03, Julia 1.9.2, Literate
This page comes from a single Julia file: 2-vector.jl
.
You can access the source code for such Julia documentation using the 'Edit on GitHub' link in the top right. You can view the corresponding notebook in nbviewer here: 2-vector.ipynb
, or open it in binder here: 2-vector.ipynb
.
Scalars, Vectors, Arrays
a = 4 # this is a scalar
4
typeof(a)
Int64
b1 = [4] # this is a Vector with one element
1-element Vector{Int64}:
4
b2 = reshape([4], 1, 1) # here is a 1×1 Array
1×1 Matrix{Int64}:
4
b3 = reshape([4], 1, 1, 1) # here is a 1×1×1 Array
1×1×1 Array{Int64, 3}:
[:, :, 1] =
4
In Julia the following all differ! (In Matlab they are the same.)
a==b1, b1==b2, a==b2, b2==b3
(false, false, false, false)
Vectors and Transpose
This construction (with just spaces) makes a 1×3 Matrix
:
c = [4 5 6]
1×3 Matrix{Int64}:
4 5 6
This construction (using commas) makes a 1D Vector
:
d = [4, 5, 6]
3-element Vector{Int64}:
4
5
6
So does this construction, whereas in Matlab the "," and ";" work differently:
e = [4; 5; 6]
3-element Vector{Int64}:
4
5
6
The transpose of a Vector
is slightly different than a 1×N
array! This is a subtle point!
d'
1×3 adjoint(::Vector{Int64}) with eltype Int64:
4 5 6
Nevertheless, the values are the same:
d' == c
true
Transposing back gives a vector again (not a N×1
array):
(d')'
3-element Vector{Int64}:
4
5
6
These are all true, as expected, despite the adjoint type:
d==e, d'==c, (c')'==d'
(true, true, true)
These are all false:
c==d, c==e, c'==d, (d')'==c'
(false, false, false, false)
An "inner product" of a 1×3 Matrix
with a 3×1 Matrix
returns a 1×1 Matrix
, not a scalar:
c * c'
1×1 Matrix{Int64}:
77
This inner product of an adjoint Matrix
with a Vector
returns a scalar:
d' * d
77
How to make a vector from an array:
vec(c)
3-element Vector{Int64}:
4
5
6
Here is another way (but it uses more memory than vec
):
c[:]
3-element Vector{Int64}:
4
5
6
Call by reference
Julia uses call-by-reference (not value), like C/C++, unlike Matlab!
Here B
is the same "pointer" so this changes A
:
A = zeros(2); B = A; B[1] = 7
A
2-element Vector{Float64}:
7.0
0.0
Here B is different, so this does not change A
:
A = zeros(2); B = A .+ 2; B[1] = 7
A
2-element Vector{Float64}:
0.0
0.0
This changes A
because B
and A
point to same data:
A = B = zeros(2); B[1] = 7
A
2-element Vector{Float64}:
7.0
0.0
This changes B
for the same reason:
A = B = zeros(2); A[1] = 7
B
2-element Vector{Float64}:
7.0
0.0
To avoid this issue, one can use copy
;
A = zeros(2); B = copy(A); B[1] = 7; # B here uses different memory than A
A # here it is unchanged
2-element Vector{Float64}:
0.0
0.0
This page was generated using Literate.jl.