Vectors in Julia - GeeksforGeeks

12 Oct, 2021
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 collection 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}
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
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 Vectorvector = [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
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 elementprintln(vector[1:2:end])
Output:
2
Geeks
Any[2, 3]
Any[1, 3, "tutorial"]
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 VectorV = [1, 2, 3, 4]   # push 5 in vectorpush!(V, 5)  # return length of vectorprintln(length(V))  # print vectorprintln(V)
Output:
5
1
2
3
4
5
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 VectorV = [1, 2, 3, 4, 5]   # remove 5 from vectorpop!(V)  # Printing vectorprintln(V)
Output:
1
2
3
4
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 VectorV = [1, 2, 3, 4]   # push 5 in vector at frontunshift!(V, 5)  # Printing vectorprintln(V)
Output:
5
1
2
3
4
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 VectorV = [1, 2, 3, 4, 5]   # remove 1 from vectorshift!(V)  # Printing Vectorprintln(V)
Output:
2
3
4
5
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 VectorV = Vector{Int64}([1, 2, 3, 4])   # append a list of items in a vectorappend!(V, [5, 6, 7])  # Printing Vectorprintln(V)
Output:
1
2
3
4
5
6
7
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 VectorV = Vector{Int64}([1, 2, 3, 4])   # print sum of vector elementprintln(sum(V))
Output:
10
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 VectorV = Vector{Int64}([1, 2, 3, 4])   # print average of vector elementprintln(mean(V))
Output:
2
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 VectorV1 = [1, 2, 3, 4, 5] V2 = [6, 7, 8, 9, 10]  # Addition of Vectorprintln(V1 + V2)  # Subtraction of Vectorprintln(V2 - V1)
Output:
Any[7, 9, 11, 13, 15]
Any[5, 5, 5, 5, 5]
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 VectorV1 = [1, 2, 3, 4, 5]   # Addition of scaler-Vectorprintln(V1 + 5)  # Multiplication of Vectorprintln(V1 * 2)
Output:
Any[6, 7, 8, 9, 10]
Any[2, 4, 6, 8, 10]
sweetyty
Picked
Julia
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Getting the maximum value from a list in Julia - max() Method
Get array dimensions and size of a dimension in Julia - size() Method
Find maximum element along with its index in Julia - findmax() Method
Decision Making in Julia (if, if-else, Nested-if, if-elseif-else ladder)
Functions in Julia
Searching in Array for a given element in Julia
Join an array of strings into a single string in Julia - join() Method
Working with Date and Time in Julia
Exception handling in Julia
Getting last element of an array in Julia - last() Method