# Creation of range along given dimension

**URL:** https://discourse.julialang.org/t/creation-of-range-along-given-dimension/56747
**Category:** General Usage
**Tags:** range
**Created:** [March 8, 2021, 4:58pm UTC](https://discourse.julialang.org/t/creation-of-range-along-given-dimension/56747 "2021-03-08T16:58:36Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![BambOoxX](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bambooxx/32/22179_2.png) [@BambOoxX](https://discourse.julialang.org/u/BambOoxX)
#### Post date: [March 8, 2021, 4:58pm UTC](https://discourse.julialang.org/t/creation-of-range-along-given-dimension/56747/1 "2021-03-08T16:58:36Z")

</div>

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:

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

```

---

<div class="post-metadata">

### Author: ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)
#### Post date: [March 8, 2021, 5:08pm UTC](https://discourse.julialang.org/t/creation-of-range-along-given-dimension/56747/2 "2021-03-08T17:08:39Z")

</div>

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`](https://bramtayl.github.io/JuliennedArrays.jl/latest/), [Tullio](https://github.com/mcabbott/Tullio.jl)).

---

<div class="post-metadata">

### Author: ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)
#### Post date: [March 8, 2021, 6:05pm UTC](https://discourse.julialang.org/t/creation-of-range-along-given-dimension/56747/3 "2021-03-08T18:05:53Z")

</div>

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

> **[GitHub - mcabbott/Tullio.jl: ⅀](https://github.com/mcabbott/Tullio.jl)**
>
> ⅀. Contribute to mcabbott/Tullio.jl development by creating an account on GitHub.

---

<div class="post-metadata">

### Author: ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)
#### Post date: [March 8, 2021, 6:22pm UTC](https://discourse.julialang.org/t/creation-of-range-along-given-dimension/56747/4 "2021-03-08T18:22:06Z")

</div>

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

> **[GitHub - JuliaArrays/RangeArrays.jl: Efficient and convenient array data...](https://github.com/JuliaArrays/RangeArrays.jl)**
>
> Efficient and convenient array data structures where the columns of the arrays are generated (on the fly) by Ranges. - GitHub - JuliaArrays/RangeArrays.jl: Efficient and convenient array data struc...

---

<div class="post-metadata">

### Author: ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)
#### Post date: [March 8, 2021, 11:47pm UTC](https://discourse.julialang.org/t/creation-of-range-along-given-dimension/56747/5 "2021-03-08T23:47:20Z")

</div>

Another perhaps relevant package here, which generalises `permutedims` (the backend of a reworking of [TensorCast.jl](https://github.com/mcabbott/TensorCast.jl)):

```julia
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

```

---

<div class="post-metadata">

### Author: ![BambOoxX](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bambooxx/32/22179_2.png) [@BambOoxX](https://discourse.julialang.org/u/BambOoxX)
#### Post date: [March 10, 2021, 9:13am UTC](https://discourse.julialang.org/t/creation-of-range-along-given-dimension/56747/6 "2021-03-10T09:13:42Z")

</div>

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