![]() |
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 VectorA 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
Creating a VectorA 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
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 elementsElements 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
Output: 2 Geeks Any[2, 3] Any[1, 3, "tutorial"] Operation on VectorsPush Operation on VectorsIt pushes the elements into a vector from the rear end. This push operation is performed with the use of a predefined push!() function. Julia
Output: 5 1 2 3 4 5 Pop Operation on VectorsIt 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
Output: 1 2 3 4 Adding elements from the Front endJulia provides a predefined function called unshift!() to push the elements into a vector from the front end. Julia
Output: 5 1 2 3 4 Removing elements from the Front EndJulia provides a predefined function called shift!() which is used to pop or remove elements from a vector from the front. Julia
Output: 2 3 4 5 Adding List of Elements to a VectorTo add a list of items into a vector, julia provides a predefined function append!(). Julia
Output: 1 2 3 4 5 6 7 Sum of Vector elementsSum of vector elements can be calculated with the use of Julia’s predefined function sum(). Julia
Output: 10 Mean of Vector ElementsTo compute the average of vector elements, Julia provides a predefined function mean() to calculate the average of elements. Julia
Output: 2 Vector Addition and Subtraction
Julia
Output: Any[7, 9, 11, 13, 15] Any[5, 5, 5, 5, 5] Scalar-Vector Addition and Multiplication
Julia
Output: Any[6, 7, 8, 9, 10] Any[2, 4, 6, 8, 10] |
Reffered: https://www.geeksforgeeks.org
Julia |
Related |
---|
![]() |
![]() |
![]() |
![]() |
![]() |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 12 |