# Cumbersome array reshaping for broadcasting, unlike numpy

**URL:** <https://discourse.julialang.org/t/cumbersome-array-reshaping-for-broadcasting-unlike-numpy/21566>\
**Category:** New to Julia\
**Created:** [March 7, 2019, 11:28am UTC](https://discourse.julialang.org/t/cumbersome-array-reshaping-for-broadcasting-unlike-numpy/21566 "2019-03-07T11:28:25Z")\
**Posts on this page:** 5\
**Page:** 2

<div class="post-metadata">

**Author:** ![bjarthur](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bjarthur/32/9638_2.png) [@bjarthur](https://discourse.julialang.org/u/bjarthur)\
**Post date:** [November 23, 2021, 4:22pm UTC](https://discourse.julialang.org/t/cumbersome-array-reshaping-for-broadcasting-unlike-numpy/21566/21 "2021-11-23T16:22:43Z")

</div>

+1 for adding `adddims` to Base. the accepted solution of indexing with `[CartesianIndex()]` is not very intuitive.

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [November 25, 2021, 9:53am UTC](https://discourse.julialang.org/t/cumbersome-array-reshaping-for-broadcasting-unlike-numpy/21566/22 "2021-11-25T09:53:48Z")

</div>

I thought `newaxis` was super weird and unnatural when I learned it in NumPy. I would probably feel the same if there was a similar magical name in Julia. In contrast I find useful to learn that `[CartesianIndex()]` works for this: it drives home some aspects of indexing that are hard to grasp otherwise, and more widely useful than this `newaxis` special case.

So I would prefer to have `[CartesianIndex()]` documented as a an idomatic example. But I don’t have much need for it, I can imagine people who do want something “nicer”…

---

<div class="post-metadata">

**Author:** ![roflmaostc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/roflmaostc/32/30123_2.png) [@roflmaostc](https://discourse.julialang.org/u/roflmaostc)\
**Post date:** [July 26, 2024, 4:14pm UTC](https://discourse.julialang.org/t/cumbersome-array-reshaping-for-broadcasting-unlike-numpy/21566/23 "2024-07-26T16:14:40Z")

</div>

Some time ago I prepared this [PR](https://github.com/JuliaLang/julia/pull/45793) to add a method to Base called `insertdims`

```julia
julia> a = [1 2; 3 4]
2×2 Matrix{Int64}:
 1 2
 3 4

julia> b = insertdims(a, dims=(1,3))
1×2×2×1 Array{Int64, 4}:
[:, :, 1, 1] =
 1 3

[:, :, 2, 1] =
 2 4

julia> b[1,1,1,1] = 5; a
2×2 Matrix{Int64}:
 5 2
 3 4

julia> b = insertdims(a, dims=(1,1))
1×1×2×2 Array{Int64, 4}:
[:, :, 1, 1] =
 5

[:, :, 2, 1] =
 3

[:, :, 1, 2] =
 2

[:, :, 2, 2] =
 4

julia> b = insertdims(a, dims=(1,2))
1×2×1×2 Array{Int64, 4}:
[:, :, 1, 1] =
 5 3

[:, :, 1, 2] =
 2 4

```

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [August 9, 2024, 9:45am UTC](https://discourse.julialang.org/t/cumbersome-array-reshaping-for-broadcasting-unlike-numpy/21566/24 "2024-08-09T09:45:36Z")

</div>

and it’s merged now 🙂

---

<div class="post-metadata">

**Author:** ![roflmaostc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/roflmaostc/32/30123_2.png) [@roflmaostc](https://discourse.julialang.org/u/roflmaostc)\
**Post date:** [August 9, 2024, 9:51am UTC](https://discourse.julialang.org/t/cumbersome-array-reshaping-for-broadcasting-unlike-numpy/21566/25 "2024-08-09T09:51:24Z")

</div>

We changed the semantics though.

Now dims indicates where the final singleton dimensions are going to be from perspective of the final result

```julia
 julia> x = [1 2 3; 4 5 6] 
 2×3 Matrix{Int64}: 
  1 2 3 
  4 5 6 
  
 julia> insertdims(x, dims=3) 
 2×3×1 Array{Int64, 3}: 
 [:, :, 1] = 
  1 2 3 
  4 5 6 
  
 julia> insertdims(x, dims=(1,2,5)) == reshape(x, 1, 1, 2, 3, 1) 
 true 
  
 julia> dropdims(insertdims(x, dims=(1,2,5)), dims=(1,2,5)) 
 2×3 Matrix{Int64}: 
  1 2 3 
  4 5 6 

```

[Previous page](https://discourse.julialang.org/t/cumbersome-array-reshaping-for-broadcasting-unlike-numpy/21566.md?page=1)
