Using Julia 1.1, I want to create a grid of points based on a vector of ranges
When using this code, it gives me a list of such points
u = [[i j ] for i in -1:0.5:1, j in -1:0.5:1]
[u...]
However, I am unable to develop a more generic approach that would create such a vector of multidimensional points when given an array of ranges
For example, develop a createGrid function that would take a Vector of n ranges as argument and output a list of all the points.
createGrid([1.:0.5:0., 1.:0.5:0., 1.:0.5:0.])
that would output:
27-element Array{Array{Float64,2},1}:
[-1.0 -1.0 -1.0]
[-0.5 -1.0 -1.0]
[0.0 -1.0 -1.0]
[-1.0 -0.5 -1.0]
[-0.5 -0.5 -1.0]
[0.0 -0.5 -1.0]
[-1.0 0.0 -1.0]
[-0.5 0.0 -1.0]
[0.0 0.0 -1.0]
[-1.0 -1.0 -0.5]
[-0.5 -1.0 -0.5]
[0.0 -1.0 -0.5]
[-1.0 -0.5 -0.5]
⋮
Thank you for any suggestion
Take a look at Iterators.product
. It doesn't create the Array
s, but creates an object you can iterate over to get each new element of the grid as a Tuple
(which is actually usually what you want).
julia> p = Iterators.product(-1.:0.5:0., -1.:0.5:0., -1.:0.5:0.);
julia> println.(p);
(-1.0, -1.0, -1.0)
(-0.5, -1.0, -1.0)
(0.0, -1.0, -1.0)
(-1.0, -0.5, -1.0)
(-0.5, -0.5, -1.0)
(0.0, -0.5, -1.0)
(-1.0, 0.0, -1.0)
⋮
I would use:
using GeometryTypes
grid(ranges::NTuple{N, <: AbstractRange}) where N = Point.(Iterators.product(ranges...))
p = grid((-1.:0.5:0.0, -1.:0.5:0.0))
By using an NTuple instead of a Vector of ranges, this methods becomes type stable (the return type gets inferred as e.g Vector{Point{2, Float64}}
).
Also, using Points is more efficient than Array{Array{Float64,1},1}
, has C-compatible layout and has all the operations defined which you need for points...
As a bonus, it directly plots in Makie / Plots as points :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With