Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you generate a regular non-integer sequence in julia?

Tags:

julia

How are regular, non-integer sequences generated in julia?

I'm trying to get 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0

In MATLAB, I would use

0.1:0.1:1

And in R

seq(0.1, 1, by = 0.1)

But I can't find anything except integer sequences in julia (e.g., 1:10). Searching for "sequence" in the docs only gives me information about how strings are sequences.

like image 630
kmm Avatar asked Feb 17 '14 23:02

kmm


1 Answers

Similarly to Matlab, but with the difference that 0.1:0.1:1 defines a Range:

julia> typeof(0.1:0.1:1)
Range{Float64} (constructor with 3 methods)

and thus if an Array is needed:

julia> [0.1:0.1:1]
10-element Array{Float64,1}:
 0.1
 0.2
 0.3
 0.4
 0.5
 0.6
 0.7
 0.8
 0.9
 1.0

Unfortunately, this use of Range is only briefly mentioned at this point of the documentation.

Edit: As mentioned in the comments by @ivarne it is possible to achieve a similar result using linspace:

julia> linspace(.1,1,10)
10-element Array{Float64,1}:
 0.1
 0.2
 0.3
 0.4
 0.5
 0.6
 0.7
 0.8
 0.9
 1.0

but note that the results are not exactly the same due to rounding differences:

julia> linspace(.1,1,10)==[0.1:0.1:1]
false
like image 69
Nico Avatar answered Oct 10 '22 01:10

Nico