gpt4 book ai didi

julia - 在 Julia 中创建长度为 n 的向量

转载 作者:行者123 更新时间:2023-12-03 15:54:57 26 4
gpt4 key购买 nike

我想创建一个长度为 n 的向量/数组之后再填充。

我怎样才能做到这一点?
它是否必须已经填充了一些东西?

最佳答案

例如,如果你想要一个长度为 10 的整数向量,你可以写

v = Vector{Int}(undef, 10)

对于维度 (2, 3, 4) 的整数数组更通用
a = Array{Int}(undef, (2, 3, 4))

请注意,这会用垃圾值填充 Vector/Array,因此这可能有点危险。作为替代方案,您可以使用
v = Vector{Int}()
sizehint!(v, 10)
push!(v, 1) # add a one to the end of the Vector
append!(v, (2, 3, 4, 5, 6, 7, 8, 9, 10)) # add values 2 to 9 to the end of the vector
sizehint!不是必需的,但它可以提高性能,因为它告诉 Julia 期望 10 个值。

还有其他功能如 zeros , onesfill可以提供已填充数据的向量/数组。

关于julia - 在 Julia 中创建长度为 n 的向量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61159913/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com