get step size of linspace

795 views
Skip to first unread message

Andrei Berceanu

unread,
Jul 1, 2014, 7:21:28 AM7/1/14
to julia...@googlegroups.com
If I define an array using the syntax

    a = [start:step:end]

how can I later recover the step? I tried step(a), but that only seems to work for integer ranges.

Ivar Nesje

unread,
Jul 1, 2014, 7:28:38 AM7/1/14
to julia...@googlegroups.com
a[2] - a[1]

Mauro

unread,
Jul 1, 2014, 7:33:34 AM7/1/14
to julia...@googlegroups.com
Why not keep the range? It should work just like an array but use less
memory:

a = start:step:end

and step works for float ranges:

julia> step(0.5:6.1:40)
6.1

Andrei Berceanu

unread,
Jul 1, 2014, 8:17:46 AM7/1/14
to julia...@googlegroups.com
Mauro, is that the only difference, the memory allocation? Can I use ranges for plotting, for instance?

Ivar Nesje

unread,
Jul 1, 2014, 8:29:26 AM7/1/14
to julia...@googlegroups.com
You can use ranges in all places where you are not calling C code, or other cases where someone explicitly restricts the type of the argument.

Mauro

unread,
Jul 1, 2014, 8:35:03 AM7/1/14
to julia...@googlegroups.com
> Mauro, is that the only difference, the memory allocation? Can I use ranges
> for plotting, for instance?

Ranges are basically just 3 numbers: start, step & stop. Just have a
look at base/range.jl

On whether they can be used instead of arrays depends on the
implementation of the function in question. However, generally
functions are implemented in terms of AbstractArray which Range (as well
as Array) is a subtype of. Just try and if it doesn't work turn it into
an array with [myrange].

Andrei Berceanu

unread,
Jul 1, 2014, 11:31:21 AM7/1/14
to julia...@googlegroups.com
And isn't there some inverse function to []?
I mean, if i have a Vector{Float64} [myrange] and want to convert it into a FloatRange{Float64} myrange.

Sam L

unread,
Jul 1, 2014, 12:27:55 PM7/1/14
to julia...@googlegroups.com
Vector{Float64} is an just array of floats in memory, and there's no way of knowing without checking that they're sorted in increasing or decreasing order and equally spaced. In order to convert from Vector{Float64} to FloatRange{Float64}, you'd have to assume that this is true or check.  If you already know that should be true, you might as well use a range in the first place.

As Mauro said, they're both AbstractArrays and can be used interchangeably in many cases, so there's not really any good reason to have such a function. It's true that ranges use less memory, but if you already have a vector sitting there you may as well use it, since the memory is already allocated.

If you wanted such a function something like this might* work:
function vec2range(v::Vector{Float64})
  issorted(v) || error("Not sorted")
  a = (v[end] - v[1])/(length(v)-1)
  for i in 2:length(v)
    isapprox(a, v[i]-v[i-1]) || error("Differences are not constant")
  end
  colon(v[1], a, v[end])
end


*Might because floats are weird. See #2333, #5636, and probably others.  It also does not work if length(v) < 2.

Andrei Berceanu

unread,
Jul 2, 2014, 5:04:32 AM7/2/14
to julia...@googlegroups.com
Its the `colon` function I was looking for, ty!
Reply all
Reply to author
Forward
0 new messages