Julia 中的向量
Julia 中的向量是元素的集合,就像数组、集合、字典等其他集合一样。向量与集合不同,因为向量是元素的有序集合,并且可以保存重复值,不像集合要求所有元素都是唯一的.向量是一维数组,并且支持与其多维对应物几乎相同的接口。
句法:
vector_name = [value1, value2, value3,..]
vector_name = Vector{DataType}([value1, value2, value3,..])
注意: Vector{T} 其中 T 是某种类型意味着与 Array{T,1} 相同。
Vector{Int}
Array{Int64,1}
# Vector{Int} = one-dimensional Vector of Int64.
Vector{Float64}
Array{Float64,1}
一维向量
一维向量或一维向量是元素的线性表示。一维向量只能有一行或一列。它代表一种可由后续内存位置访问的列表类型。矢量可以调整大小。可以在矢量的前面或后面添加或删除元素。
Julia
A = [1, 2, 3]
3-element Array{Int64,1}:
1
2
3
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)
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])
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)
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)
Julia
# Julia program to illustrate
# the use of Vector
# Creating a 1D Vector
V = [1, 2, 3, 4]
# push 5 in vector at front
unshift!(V, 5)
# Printing vector
println(V)
Julia
# Julia program to illustrate
# the use of Vector
# Creating a 1D Vector
V = [1, 2, 3, 4, 5]
# remove 1 from vector
shift!(V)
# Printing Vector
println(V)
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)
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))
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))
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)
# Substraction of Vector
println(V2 - V1)
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)
创建向量
Julia 中的 Vector 可以使用预定义的关键字Vector()或通过简单地将 Vector 元素写入方括号 ([]) 中来创建。有多种创建 Vector 的方法。
vector_name = [value1, value2, value3,..]
or
vector_name = Vector{Datatype}([value1, value2, value3,..])
朱莉娅
# 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)
输出:
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
访问向量元素
可以通过将向量中值的索引作为参数传递给 vector_name 来访问向量的元素。此索引在“[]”内传递。可以通过使用“:”传递索引范围来访问一系列向量元素。
示例:访问向量中的元素
朱莉娅
# 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])
输出:
2
Geeks
Any[2, 3]
Any[1, 3, "tutorial"]
对向量的操作
向量上的推送操作
它从后端将元素推入一个向量中。此推送操作是使用预定义的push!()函数的。
朱莉娅
# 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)
输出:
5
1
2
3
4
5
向量上的流行操作
它用于从后端弹出或删除向量中的元素。这个弹出操作是通过使用pop!()函数的。
朱莉娅
# 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)
输出:
1
2
3
4
从前端添加元素
Julia 提供了一个名为unshift!()的预定义函数,用于将元素从前端推送到向量中。
朱莉娅
# Julia program to illustrate
# the use of Vector
# Creating a 1D Vector
V = [1, 2, 3, 4]
# push 5 in vector at front
unshift!(V, 5)
# Printing vector
println(V)
输出:
5
1
2
3
4
从前端移除元素
Julia 提供了一个名为shift!()的预定义函数,用于从向量的前面弹出或删除元素。
朱莉娅
# Julia program to illustrate
# the use of Vector
# Creating a 1D Vector
V = [1, 2, 3, 4, 5]
# remove 1 from vector
shift!(V)
# Printing Vector
println(V)
输出:
2
3
4
5
将元素列表添加到向量
要将项目列表添加到向量中,julia 提供了一个预定义的函数append!()。
朱莉娅
# 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)
输出:
1
2
3
4
5
6
7
向量元素的总和
可以使用 Julia 的预定义函数sum()计算向量元素的总和。
朱莉娅
# 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))
输出:
10
向量元素的均值
为了计算向量元素的平均值,Julia 提供了一个预定义的函数mean()来计算元素的平均值。
朱莉娅
# 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))
输出:
2
矢量加法和减法
- 矢量加法使用“+”,矢量减法使用“-”。
- 数组必须具有相同的长度
朱莉娅
# 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)
# Substraction of Vector
println(V2 - V1)
输出:
Any[7, 9, 11, 13, 15]
Any[5, 5, 5, 5, 5]
标量向量加法和乘法
- 标量被添加到向量的每个条目。
- 标量向量乘法使用 *
朱莉娅
# 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)
输出:
Any[6, 7, 8, 9, 10]
Any[2, 4, 6, 8, 10]