Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Transform a LinRange in array in Julia

I am new to Julia and I am trying to transform a LinRange to Array for further calculations. For example I have:

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

With output:

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

Then I transform it in array using:

julia> x = [x]

With output:

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

But when I try to access it as a normal array

julia> x[1]

I have the entire LinRange as output:

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

And if I try to access the second element I get this error:

julia> x[2] 

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

I understand that I should "go a level down", but how can I do it? Trying for example with x[1,1] outputs always the entire LinRange.

like image 470
BlackPhoenix Avatar asked Jul 24 '26 04:07

BlackPhoenix


1 Answers

Use:

collect(x)

or

vcat(x)

or

[x;]

However, the question is why do you need a Vector. Unless you need to mutate it it is more efficient to work with LinRange.

like image 111
Bogumił Kamiński Avatar answered Jul 26 '26 04:07

Bogumił Kamiński



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!