📜  用 Julia 中的特定值填充数组 |数组填充()方法

📅  最后修改于: 2022-05-13 01:55:33.298000             🧑  作者: Mango

用 Julia 中的特定值填充数组 |数组填充()方法

fill()是 julia 中的一个内置函数,用于返回一个指定维度的数组,其中填充了作为参数传递给它的特定值。

例子:

Python
# Julia program to illustrate
# the use of Array fill() method
 
# Creating a 1D array of size 4
# with each element filled with value 5
A = fill(5, 4)
println(A)
 
# Creating a 2D array of size 2X3
# with each element filled with value 5
B = fill(5, (2, 3))
println(B)
 
# Creating a 3D array of size 2X2X2
# with each element filled with value 5
C = fill(5, (2, 2, 2))
println(C)


Python
# Julia program to illustrate
# the use of Array fill() method
 
# Creating a 1D array of size 5
Array1 = [1, 2, 3, 4, 5]
 
# Filling array with fill!()
Array1 = fill!(Array1, 10)
println(Array1)
 
# Creating a 2D array of size 2X2
Array2 = [1 2; 3 4]
Array2 = fill!(Array2, 10)
println(Array2)
 
# Creating a 3D array of size 2X2X2
Array3 = cat([1 2; 3 4], [5, 6; 7 8], dims=3)
Array3 = fill!(Array3, 10)
println(Array3)


输出:

填充-朱莉娅-02

数组填充!() 方法

fill!()方法的工作原理与fill()方法完全相同,即它使用作为参数传递给它的特定值填充数组,但唯一的区别是, fill!()方法将现有数组作为参数并用新的指定值。而fill()方法采用数组维度并创建自己的新数组。

示例:下面的代码使用 3 个元素的一维数组。

Python

# Julia program to illustrate
# the use of Array fill() method
 
# Creating a 1D array of size 5
Array1 = [1, 2, 3, 4, 5]
 
# Filling array with fill!()
Array1 = fill!(Array1, 10)
println(Array1)
 
# Creating a 2D array of size 2X2
Array2 = [1 2; 3 4]
Array2 = fill!(Array2, 10)
println(Array2)
 
# Creating a 3D array of size 2X2X2
Array3 = cat([1 2; 3 4], [5, 6; 7 8], dims=3)
Array3 = fill!(Array3, 10)
println(Array3)

输出:

填充-朱莉娅-01