Vectors in Julia are a collection of elements just like other collections like Array, Sets, Dictionaries, etc. Vector are different from Sets because vectors are ordered collections of elements, and can hold duplicate values, unlike sets which require all the elements to be unique. Vectors are one-dimensional arrays, and support mostly the same interface as their multi-dimensional counterparts.
Syntax:
vector_name = [value1, value2, value3,..]
vector_name = Vector{DataType}([value1, value2, value3,..])
Note: Vector{T} where T is some type means the same as Array{T,1}.
Vector{Int}
Array{Int64,1}
# Vector{Int} = one-dimensional Vector of Int64.
Vector{Float64}
Array{Float64,1}
1D Vector
A 1D Vector or 1-dimensional Vector is a linear representation of elements. A 1D Vector can only have either a row or a column. It represents a type of list that can be accessed by subsequent memory locations. Vectors can be resized. Elements can be added or removed from the front or back of the vector.
Julia
A = [ 1 , 2 , 3 ]
3 - element Array{Int64, 1 }:
1
2
3
|
Creating a Vector
A Vector in Julia can be created with the use of a pre-defined keyword Vector() or by simply writing Vector elements within square brackets([]). There are different ways of creating Vector.
vector_name = [value1, value2, value3,..]
or
vector_name = Vector{Datatype}([value1, value2, value3,..])
Julia
vector = [ 1 , 2 , 3 , 4 ]
println(vector)
Vector{Float64}(undef, 3 )
|
Output:
julia> vector = [1, 2, 3, 4]
4-element Array{Int64,1}:
1
2
3
4
julia> Vector{Float64}(undef, 3)
3-element Array{Float64,1}:
6.90966e-310
6.90966e-310
6.90966e-310
Accessing Vector elements
Elements of a vector can be accessed by passing the index of the value in the vector as a parameter to the vector_name. This index is passed within ‘[ ]’. A range of vector elements can be accessed by passing the index range with the use of ‘:’.
Example: Accessing elements in a Vector
Julia
vector = [ 1 , 2 , 3 , "Geeks" , "tutorial" , "Geeks" ]
println(vector[ 2 ])
println(vector[end])
println(vector[ 2 : 3 ])
println(vector[ 1 : 2 :end])
|
Output:
2
Geeks
Any[2, 3]
Any[1, 3, "tutorial"]
Operation on Vectors
Push Operation on Vectors
It pushes the elements into a vector from the rear end. This push operation is performed with the use of a predefined push!() function.
Julia
V = [ 1 , 2 , 3 , 4 ]
push!(V, 5 )
println(length(V))
println(V)
|
Output:
5
1
2
3
4
5
Pop Operation on Vectors
It is used to pop or remove elements from a vector from the rear end. This pop operation is performed with the use of pop!() function.
Julia
V = [ 1 , 2 , 3 , 4 , 5 ]
pop!(V)
println(V)
|
Output:
1
2
3
4
Adding elements from the Front end
Julia provides a predefined function called unshift!() to push the elements into a vector from the front end.
Julia
V = [ 1 , 2 , 3 , 4 ]
pushfirst!(V, 5 )
println(V)
|
Output:
5
1
2
3
4
Removing elements from the Front End
Julia provides a predefined function called shift!() which is used to pop or remove elements from a vector from the front.
Julia
V = [ 1 , 2 , 3 , 4 , 5 ]
popfirst!(V)
println(V)
|
Output:
2
3
4
5
Adding List of Elements to a Vector
To add a list of items into a vector, julia provides a predefined function append!().
Julia
V = Vector{Int64}([ 1 , 2 , 3 , 4 ])
append!(V, [ 5 , 6 , 7 ])
println(V)
|
Output:
1
2
3
4
5
6
7
Sum of Vector elements
Sum of vector elements can be calculated with the use of Julia’s predefined function sum().
Julia
V = Vector{Int64}([ 1 , 2 , 3 , 4 ])
println( sum (V))
|
Output:
10
Mean of Vector Elements
To compute the average of vector elements, Julia provides a predefined function mean() to calculate the average of elements.
Julia
V = Vector{Int64}([ 1 , 2 , 3 , 4 ])
println(mean(V))
|
Output:
2
Vector Addition and Subtraction
- Vector addition uses ‘+’ and vector subtraction uses ‘ -‘.
- The arrays must have the same length
Julia
V1 = [ 1 , 2 , 3 , 4 , 5 ]
V2 = [ 6 , 7 , 8 , 9 , 10 ]
println(V1 + V2)
println(V2 - V1)
|
Output:
Any[7, 9, 11, 13, 15]
Any[5, 5, 5, 5, 5]
Scalar-Vector Addition and Multiplication
- The scalar is added to each entry of the vector.
- Scalar-vector multiplication uses *
Julia
V1 = [ 1 , 2 , 3 , 4 , 5 ]
println(V1 + 5 )
println(V1 * 2 )
|
Output:
Any[6, 7, 8, 9, 10]
Any[2, 4, 6, 8, 10]
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!