Julia Vectors

Vectors are a columnar series of values.


Description

Vectors are instantiated like:

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

Vectors cannot ordinarily be created from a range, but using the vertical concatenation operator (;) can be a trick to make it work anyway. Consider:

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

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

Typing

Vectors are required to be a collection of values that are of the same type. If those values are not of the same type but have a common promotion type, they are type cast.

julia> a = [1, 2.3, 4//5]
3-element Vector{Float64}:
 1.0
 2.3
 0.8

The values can also be explicitly type cast when instantiated.

julia> Float32[1, 2.3, 4//5]
3-element Vector{Float32}:
 1.0
 2.3
 0.8

The Vector type itself descends from AbstractArray, just like the Matrix type.

Comparison to Row Vectors

While columnar series are of type Vector, row series are of type Matrix and inherently are 2-dimensional. Most vector operations will not work on a row 'vector'. This is a simple mistake to make; consider:

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

julia> b = [1 2 3]
1×3 Matrix{Int64}:
 1  2  3

To flatten a 2-dimensional matrix into a vector, try the vec() function.

Note that when given an actual Matrix argument, the columns are extracted and stacked.

julia> b = [1 2 3; 4 5 6]
2×3 Matrix{Int64}:
 1  2  3
 4  5  6

julia> c = vec(b)
6-element Vector{Int64}:
 1
 4
 2
 5
 3
 6


CategoryRicottone

Julia/Vectors (last edited 2026-01-18 23:06:13 by DominicRicottone)