Open In App

Vectors in Julia

Last Updated : 04 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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




# Julia program to illustrate 
# the use of Vector
   
# Creating a 1D Vector
vector = [1, 2, 3, 4]
println(vector)
 
# Vector{T}(undef, n)
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




# Julia program to illustrate 
# the use of Vector
   
# Creating a Vector
vector = [1, 2, 3, "Geeks", "tutorial", "Geeks"]
   
# Passing index value
println(vector[2])
 
# Accessing last value
println(vector[end])
 
# Passing a range of indices
println(vector[2:3]) # selects the second and third elements
 
# Access every other element
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




# Julia program to illustrate 
# the use of Vector
   
# Creating a 1D Vector
V = [1, 2, 3, 4]
 
# push 5 in vector
push!(V, 5)
 
# return length of vector
println(length(V))
 
# print vector
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




# Julia program to illustrate 
# the use of Vector
   
# Creating a 1D Vector
V = [1, 2, 3, 4, 5]
 
# remove 5 from vector
pop!(V)
 
# Printing vector
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




# Julia program to illustrate 
# the use of Vector
   
# Creating a 1D Vector
V = [1, 2, 3, 4]
 
# push 5 in vector at front
pushfirst!(V, 5)
 
# Printing vector
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




# Julia program to illustrate 
# the use of Vector
   
# Creating a 1D Vector
V = [1, 2, 3, 4, 5]
 
# remove 1 from vector
popfirst!(V)
 
# Printing Vector
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




# Julia program to illustrate 
# the use of Vector
   
# Creating a 1D Vector
V = Vector{Int64}([1, 2, 3, 4])
 
# append a list of items in a vector
append!(V, [5, 6, 7])
 
# Printing Vector
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




# Julia program to illustrate 
# the use of Vector
   
# Creating a 1D Vector
V = Vector{Int64}([1, 2, 3, 4])
 
# print sum of vector element
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




# Julia program to illustrate 
# the use of Vector
   
# Creating a 1D Vector
V = Vector{Int64}([1, 2, 3, 4])
 
# print average of vector element
println(mean(V))


Output:

2

Vector Addition and Subtraction

  • Vector addition uses ‘+’ and vector subtraction uses ‘ -‘.
  • The arrays must have the same length

Julia




# Julia program to illustrate 
# the use of Vector
   
# Creating a 1D Vector
V1 = [1, 2, 3, 4, 5]
V2 = [6, 7, 8, 9, 10]
 
# Addition of Vector
println(V1 + V2)
 
# Subtraction of Vector
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




# Julia program to illustrate 
# the use of Vector
   
# Creating a 1D Vector
V1 = [1, 2, 3, 4, 5]
 
# Addition of scaler-Vector
println(V1 + 5)
 
# Multiplication of Vector
println(V1 * 2)


Output:

Any[6, 7, 8, 9, 10]
Any[2, 4, 6, 8, 10]


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads