在 Julia 中创建类似类型的数组——similar() 方法
Similar similar()
是 julia 中的内置函数,用于根据给定的源数组创建具有给定元素类型和大小的未初始化可变数组。
Syntax:
similar(array, element_type=eltype(array), dims=size(array))
Parameters:
- array: Specified arrays.
- element_type: Specified type of elements.
- dims=size(array): Specified size of elements.
Returns: It returns an uninitialized mutable array with the given element type and size.
示例 1:
# Julia program to illustrate
# the use of Array similar() method
# Getting an uninitialized mutable array
# with the given element type and size
A = [1, 2, 3, 4];
similar(A, 2, 4)
similar(2:8)
similar(2:8, 1)
similar(2:8, 1, 4)
输出:
示例 2:
# Julia program to illustrate
# the use of Array similar() method
# Getting an uninitialized mutable array
# with the given element type and size
println(similar(trues(2, 6), 3))
println(similar(falses(5), 1, 3))
输出: