Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

StepRange description in Julia

Tags:

julia

while working in julia programming, for creating an array instead of using a=[1:1:20...] i used a=[1:1:20] and it created an array saying "1-element Array{StepRange{Int64,Int64},1}". What does this "1-element Array{StepRange{Int64,Int64},1}" mean? what StepRange means?

like image 660
Aakash Srinivasan Avatar asked May 07 '26 03:05

Aakash Srinivasan


1 Answers

From the documentation of StepRange (type ?StepRange in the Julia REPL to see this):

StepRange{T, S} <: OrdinalRange{T, S}

Ranges with elements of type T with spacing of type S. The step between each element is constant, and the range is defined in terms of a start and stop of type T and a step of type S. Neither T nor S should be floating point types. The syntax a:b:c with b > 1 and a, b, and c all integers creates a StepRange.

So, for example

julia> typeof(1:1:20)
StepRange{Int64,Int64}

and

julia> [1:1:20]
1-element Array{StepRange{Int64,Int64},1}:
 1:1:20

thus constructs a Vector (1D Array) containing one StepRange. If you want to materialize the lazy StepRange I would recommend collect(1:1:20) instead of using splatting ([1:1:20...]).

You can access start / step / stop fields of a StepRange using:

julia> r = 1:1:20
julia> r.start
1

julia> r.stop
20

julia> r.step
1
like image 63
fredrikekre Avatar answered May 11 '26 13:05

fredrikekre



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!