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 notebook 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
a = 4 # this is a scalar
4
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
a==b1, b1==b2, a==b2, b2==b3 # in Julia these all differ! (in Matlab they are the same)
(false, false, false, false)
c = [4 5 6] # this construction (with just spaces) makes a 1×3 Array
1×3 Matrix{Int64}: 4 5 6
d = [4, 5, 6] # this construction (using commas) makes a 1D Vector
3-element Vector{Int64}: 4 5 6
e = [4; 5; 6] # so does this construction, whereas in Matlab the "," and ";" work differently
3-element Vector{Int64}: 4 5 6
d' # the transpose of a Vector is slightly different than a 1×N array! This is subtle!
1×3 adjoint(::Vector{Int64}) with eltype Int64: 4 5 6
d' == c # nevertheless, the values are the same...
true
(d')' # Transposing back gives a vector again (not a N×1 array)
3-element Vector{Int64}: 4 5 6
d==e, d'==c, (c')'==d' # These are all true, as expected, despite the adjoint type.
(true, true, true)
c==d, c==e, c'==d, (d')'==c' # These are all false.
(false, false, false, false)
c * c' # "inner product" of a 1×3 Array with a 3×1 Array returns a 1×1 Array, not a scalar
1×1 Matrix{Int64}: 77
d' * d # This inner product of an Adjoint Vector with a Vector returns a scalar
77
vec(c) # how to make a vector from an array
3-element Vector{Int64}: 4 5 6
c[:] # Here is another way (but it uses more memory than vec)
3-element Vector{Int64}: 4 5 6
Julia uses call-by-reference (not value), like C/C++, unlike Matlab!
A = zeros(2); B = A; B[1] = 7; # B is the same "pointer" so this changes A
@show A;
A = [7.0, 0.0]
A = zeros(2); B = A .+ 2; B[1] = 7; # here B is different so this does not change A
@show A;
A = [0.0, 0.0]
A = B = zeros(2); B[1] = 7; # changes A because B and A point to same data
@show A;
A = [7.0, 0.0]
A = B = zeros(2); A[1] = 7; # changes B for the same reason
@show B;
B = [7.0, 0.0]
To avoid this issue, one can use copy
.
It is only needed rarely though.
A = zeros(2); B = copy(A); B[1] = 7; # B here uses different memory than A
@show A; # here it is unchanged
A = [0.0, 0.0]