Creation of range along given dimension

I currently have to handle multi-dimensional data, where I have to compute several broadcasting operations with ranges.

In 2D I use e.g (1:10) .* (1:5)' which gives a nice matrix.
The first vector is of type UnitRange{T} and the second results in a Adjoint{T,UnitRange{T}} (with here T = Int64).

What would be the recommended way to broadcast theses ranges to higher dimensions ?

I thought about initializing a matrix of singleton dimensions except one then fill it with a range, but it feels a bit weird.
For instance:

ndrange = zeros(1,1,1,10,1)
ndrange[1,1,1,:,1] = 1:10

You can use reshape(1:10, 1, 1, 1, 10, 1) for broadcasting, but depending on what you’re doing, there may be better options (plain loops, mapslices, JuliennedArrays, Tullio).

2 Likes

As @stillyslalom said, I would appeal to something beyond the usual dot-broadcasting for this. mapslices or Tullio.jl are great alternatives.

1 Like

It might be easier and more efficient to just have range-like higher dimensional arrays. E.g., the rather barebones

1 Like

Another perhaps relevant package here, which generalises permutedims (the backend of a reworking of TensorCast.jl):

julia> using TransmuteDims

julia> transmutedims('a':'e', (2,1))  # makes an Array, knows that size('a':'e',2)==1
1×5 Array{Char,2}:
 'a'  'b'  'c'  'd'  'e'

julia> transmute(10:10:30, (3,2,1))  # lazy version, notice (3,2,1) equivalent to (0,0,1)
1×1×3 transmute(::StepRange{Int64,Int64}, (0, 0, 1)) with eltype Int64:
[:, :, 1] =
 10

[:, :, 2] =
 20

[:, :, 3] =
 30
1 Like

Well I guess, Tullio would probably be very useful! I will have a look at it ! Thanks everyone for the help