在 Julia 中转换数组中的 LinRange

Transform a LinRange in array in Julia

我是 Julia 的新手,我正在尝试将 LinRange 转换为 Array 以进行进一步计算。例如我有:

julia> x = LinRange(0, 1, 100) 

输出:

100-element LinRange{Float64, Int64}: 0.0,0.010101,0.020202,0.030303,0.040404,0.0505051,0.0606061,0.0707071,0.0808081,0.0909091,0.10101,…,0.89899,0.909091,0.919192,0.929293,0.939394,0.949495,0.959596,0.969697,0.979798,0.989899,1.0

然后我使用以下方法将其转换为数组:

julia> x = [x]

输出:

1-element Vector{LinRange{Float64, Int64}}:  range(0.0, stop=1.0, length=100)

但是当我尝试将其作为普通数组访问时

julia> x[1]

我将整个 LinRange 作为输出:

100-element LinRange{Float64, Int64}:  0.0,0.010101,0.020202,0.030303,0.040404,0.0505051,0.0606061,0.0707071,0.0808081,0.0909091,0.10101,…,0.89899,0.909091,0.919192,0.929293,0.939394,0.949495,0.959596,0.969697,0.979798,0.989899,1.0

如果我尝试访问第二个元素,我会收到此错误:

julia> x[2] 

ERROR: BoundsError: attempt to access 1-element Vector{LinRange{Float64, Int64}} at index [2]

我知道我应该“降级”,但我该怎么做呢?例如尝试使用 x[1,1] 输出总是整个 LinRange.

使用:

collect(x)

vcat(x)

[x;]

然而,问题是你为什么需要Vector。除非你需要改变它,否则使用 LinRange.

会更有效