# Broadcasting arrays with different number of dimension

**URL:** https://discourse.julialang.org/t/broadcasting-arrays-with-different-number-of-dimension/58973
**Category:** General Usage
**Tags:** broadcast, arrays
**Created:** [April 10, 2021, 9:31am UTC](https://discourse.julialang.org/t/broadcasting-arrays-with-different-number-of-dimension/58973 "2021-04-10T09:31:31Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Sunny](https://avatars.discourse-cdn.com/v4/letter/s/7c8e57/32.png) [@Sunny](https://discourse.julialang.org/u/Sunny)
#### Post date: [April 10, 2021, 9:31am UTC](https://discourse.julialang.org/t/broadcasting-arrays-with-different-number-of-dimension/58973/1 "2021-04-10T09:31:31Z")

</div>

In PyTorch it’s possible to do the operation:

```julia
# v.shape = torch.Size([1, 2, 1])
# b = torch.Size([2, 8])
a = v - b
# a.shape = torch.Size([1, 2, 8])

```

However when I try to implement this in Julia  
I get an error

```julia
rand(1, 2, 1)-rand(2,8)

DimensionMismatch("dimensions must match: a has dims (Base.OneTo(1), Base.OneTo(2), Base.OneTo(1)), b has dims (Base.OneTo(2), Base.OneTo(8)), mismatch at 1")

Stacktrace:
 [1] promote_shape at ./indices.jl:178 [inlined]
 [2] promote_shape(::Array{Float64,3}, ::Array{Float64,2}) at ./indices.jl:169
 [3] -(::Array{Float64,3}, ::Array{Float64,2}) at ./arraymath.jl:38
 [4] top-level scope at In[318]:1
 [5] include_string(::Function, ::Module, ::String, ::String) at ./loading.jl:1091

```

What is the right way to do this?

---

<div class="post-metadata">

### Author: ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)
#### Post date: [April 10, 2021, 9:43am UTC](https://discourse.julialang.org/t/broadcasting-arrays-with-different-number-of-dimension/58973/2 "2021-04-10T09:43:26Z")

</div>

> What is the right way to do this?

That depends. What is it supposed to do?

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [April 10, 2021, 9:48am UTC](https://discourse.julialang.org/t/broadcasting-arrays-with-different-number-of-dimension/58973/3 "2021-04-10T09:48:04Z")

</div>

First of all you have to use broadcasting, that is, dot the minus sign:

```julia
v .- b

```

This still won’t work, since the second dimension of the arrays are 2 and 8, respectively. They are incompatible, so maybe you can explain what pytorch is doing, it doesn’t make sense to me.

---

<div class="post-metadata">

### Author: ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)
#### Post date: [April 10, 2021, 9:50am UTC](https://discourse.julialang.org/t/broadcasting-arrays-with-different-number-of-dimension/58973/4 "2021-04-10T09:50:00Z")

</div>

Python has row major storage, so it matches the dimensions from the right. Julia has column major storage, but matching the same dimensions from the left doesn’t work. You can permute the dimensions for example.

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [April 10, 2021, 10:15am UTC](https://discourse.julialang.org/t/broadcasting-arrays-with-different-number-of-dimension/58973/5 "2021-04-10T10:15:23Z")

</div>

That is mindbending🤯 But ‘row-major’ needs a new name. ‘Last-dimension-major’, or something.

---

<div class="post-metadata">

### Author: ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)
#### Post date: [April 10, 2021, 10:33am UTC](https://discourse.julialang.org/t/broadcasting-arrays-with-different-number-of-dimension/58973/6 "2021-04-10T10:33:01Z")

</div>

I griped about that once, last dimension contiguous was suggested

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [April 10, 2021, 12:32pm UTC](https://discourse.julialang.org/t/broadcasting-arrays-with-different-number-of-dimension/58973/7 "2021-04-10T12:32:56Z")

</div>

The easiest way is just to drop the unit dimensions of `v` before broadcasting, either by constructing `v` as a 1d array to start with or by calling `dropdims` (which is essentially free because it just calls `reshape` under the hood, sharing data):

```julia
julia> dropdims(v, dims=3) .- b

julia> dropdims(v, dims=(1,3)) .- b
2×8 Matrix{Float64}:
 -0.178031 -0.593356 -0.885534 … -0.526637 -0.0416247 -0.579015
 -0.368148 -0.883498 -0.693581 -0.372224 -0.708516 -0.522531

```

---

<div class="post-metadata">

### Author: ![Sunny](https://avatars.discourse-cdn.com/v4/letter/s/7c8e57/32.png) [@Sunny](https://discourse.julialang.org/u/Sunny)
#### Post date: [April 10, 2021, 1:17pm UTC](https://discourse.julialang.org/t/broadcasting-arrays-with-different-number-of-dimension/58973/8 "2021-04-10T13:17:18Z")

</div>

The first dimenstion of rand(1,2,1) is supposed to be batch size.  
I’m currently using this for test

```julia
b = reshape(rand(2,8), (1, 2, 8))
rand(1,2,1) .- b

```

If it breaks I’ll try methods suggested here,  
thanks for support
