# Subtract Vector from Vector of Vectors

**URL:** https://discourse.julialang.org/t/subtract-vector-from-vector-of-vectors/96913
**Category:** General Usage
**Created:** [March 31, 2023, 8:19pm UTC](https://discourse.julialang.org/t/subtract-vector-from-vector-of-vectors/96913 "2023-03-31T20:19:34Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![liamfdoherty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/liamfdoherty/32/24516_2.png) [@liamfdoherty](https://discourse.julialang.org/u/liamfdoherty)
#### Post date: [March 31, 2023, 8:19pm UTC](https://discourse.julialang.org/t/subtract-vector-from-vector-of-vectors/96913/1 "2023-03-31T20:19:34Z")

</div>

I’m having a bit of a moment. What is wrong with

```julia
julia> x = [[1, 2, 3], [4, 5, 6]]
2-element Vector{Vector{Int64}}:
 [1, 2, 3]
 [4, 5, 6]
julia> x .- [1, 2, 3]
ERROR: DimensionMismatch: arrays could not be broadcast to a common size; got a dimension with lengths 2 and 3
Stacktrace:
 [1] _bcs1
   @ ./broadcast.jl:516 [inlined]
 [2] _bcs
   @ ./broadcast.jl:510 [inlined]
 [3] broadcast_shape
   @ ./broadcast.jl:504 [inlined]
 [4] combine_axes
   @ ./broadcast.jl:499 [inlined]
 [5] instantiate
   @ ./broadcast.jl:281 [inlined]
 [6] materialize(bc::Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{1}, Nothing, typeof(-), Tuple{Vector{Vector{Int64}}, Vector{Int64}}})
   @ Base.Broadcast ./broadcast.jl:860
 [7] top-level scope
   @ REPL[18]:1

```

? I would like to see

```julia
[[0, 0, 0], [3, 3, 3]]

```

but I’m obviously doing something silly.

---

<div class="post-metadata">

### Author: ![mikmoore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikmoore/32/31109_2.png) [@mikmoore](https://discourse.julialang.org/u/mikmoore)
#### Post date: [March 31, 2023, 8:29pm UTC](https://discourse.julialang.org/t/subtract-vector-from-vector-of-vectors/96913/2 "2023-03-31T20:29:51Z")

</div>

The issue is that you have a 2-vector of 3-vectors and you try to elementwise subtract a 3-vector. Broadcasting semantics mean you’re trying to compute the 2-vector (of 3-vectors) minus a 3-vector. Since 2 != 3, this doesn’t make sense and you get the error.

What you’re looking for is to subtract the 3-vector _from each_ of the 3-vectors in the 2-vector. This means you want to broadcast the 2-vector _but not_ the 3-vector. You can use `Ref` (or a 1-element array, a 1-element tuple, or a few other constructs) to prevent broadcasting of a collection. When used, it broadcast over the 1-element `Ref` (or array or tuple…) instead of the inner contents. Like this:

```julia
[[1, 2, 3], [4, 5, 6]] .- Ref([1, 2, 3])

```

---

<div class="post-metadata">

### Author: ![liamfdoherty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/liamfdoherty/32/24516_2.png) [@liamfdoherty](https://discourse.julialang.org/u/liamfdoherty)
#### Post date: [April 4, 2023, 5:49pm UTC](https://discourse.julialang.org/t/subtract-vector-from-vector-of-vectors/96913/3 "2023-04-04T17:49:04Z")

</div>

This works for subtraction, but it doesn’t for division. Understandably, because “division by a vector” is not exactly clear, but what I want to do now is elementwise division of vectors, e.g.,

```julia
julia> [1., 2.] ./ [3., 4.]
2-element Vector{Float64}:
 0.3333333333333333
 0.5

```

If I change the solution above to division instead, I get

```julia
julia> [[1, 2, 3], [4, 5, 6]] ./ Ref([1, 2, 3])
2-element Vector{Matrix{Float64}}:
 [0.07142857142857142 0.14285714285714285 0.21428571428571427; 0.14285714285714285 0.2857142857142857 0.42857142857142855; 0.21428571428571427 0.42857142857142855 0.6428571428571428]
 [0.2857142857142857 0.5714285714285714 0.8571428571428571; 0.3571428571428571 0.7142857142857142 1.0714285714285714; 0.42857142857142855 0.8571428571428571 1.2857142857142856]

```

which is not what I intend or expect. I can do instead

```julia
julia> x = [[1, 2, 3], [4, 5, 6]]
2-element Vector{Vector{Int64}}:
 [1, 2, 3]
 [4, 5, 6]

julia> [xx ./ [1, 2, 3] for xx in x]
2-element Vector{Vector{Float64}}:
 [1.0, 1.0, 1.0]
 [4.0, 2.5, 2.0]

```

to circumvent the issue, but this feels a little clunky. Is there a cleaner way, nearer the spirit of the above solution for division?

---

<div class="post-metadata">

### Author: ![adienes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adienes/32/37459_2.png) [@adienes](https://discourse.julialang.org/u/adienes)
#### Post date: [April 4, 2023, 6:17pm UTC](https://discourse.julialang.org/t/subtract-vector-from-vector-of-vectors/96913/4 "2023-04-04T18:17:16Z")

</div>

maybe

```julia
vdiv(a, b) = a ./ b
vdiv.(x, Ref([1,2,3]))

```

or

`map(r -> r ./ [1, 2, 3], x)`

I think the problem is that `/(::Vector, ::Vector)` is already defined but it doesn’t do element-wise division like you’re looking for

---

<div class="post-metadata">

### Author: ![mikmoore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikmoore/32/31109_2.png) [@mikmoore](https://discourse.julialang.org/u/mikmoore)
#### Post date: [April 4, 2023, 6:50pm UTC](https://discourse.julialang.org/t/subtract-vector-from-vector-of-vectors/96913/5 "2023-04-04T18:50:21Z")

</div>

~~There isn’t a fancy syntax for nested broadcasting~~ (EDIT: See the following post for fancy syntax), but you can construct it using the `broadcast` function over broadcasted division `./` like this:

```julia-repl
julia> broadcast(./, [[1, 2, 3], [4, 5, 6]], Ref([1, 2, 3]))
2-element Vector{Vector{Float64}}:
 [1.0, 1.0, 1.0]
 [4.0, 2.5, 2.0]

```

The subtraction solution above `[[1, 2, 3], [4, 5, 6]] .- Ref([1, 2, 3])` is equivalent to `broadcast(-, [[1, 2, 3], [4, 5, 6]], Ref([1, 2, 3]))`. It is also in-practice equivalent to `broadcast(.-, [[1, 2, 3], [4, 5, 6]], Ref([1, 2, 3]))`, but only because `-` and `.-` are equivalent on pairs of vectors (unlike `/` and `./`).

---

<div class="post-metadata">

### Author: ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)
#### Post date: [April 4, 2023, 7:03pm UTC](https://discourse.julialang.org/t/subtract-vector-from-vector-of-vectors/96913/6 "2023-04-04T19:03:51Z")

</div>

A somewhat simpler-looking way is

```julia
julia> (./).([[1, 2, 3], [4, 5, 6]], Ref([1, 2, 3]))
2-element Vector{Vector{Float64}}:
 [1.0, 1.0, 1.0]
 [4.0, 2.5, 2.0]

```

But yeah, convenient interface for deeper broadcasting would be nice…

---

<div class="post-metadata">

### Author: ![mikmoore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikmoore/32/31109_2.png) [@mikmoore](https://discourse.julialang.org/u/mikmoore)
#### Post date: [April 4, 2023, 7:34pm UTC](https://discourse.julialang.org/t/subtract-vector-from-vector-of-vectors/96913/7 "2023-04-04T19:34:39Z")

</div>

For unlimited-depth broadcasting, it’s often easiest to create a function that broadcasts over itself. Something like

```julia
dotplus(a, b) = dotplus.(a, b)
dotplus(a::Number, b::Number) = +(a, b) # base case

x = fill(fill([1 3; 2 4],2),3)
y = [50 70; 60 80]

dotplus(x,Ref(y))
# 3-element Vector{Matrix{Matrix{Int64}}}:
# [[51 53; 52 54] [71 73; 72 74]; [61 63; 62 64] [81 83; 82 84]]
# [[51 53; 52 54] [71 73; 72 74]; [61 63; 62 64] [81 83; 82 84]]
# [[51 53; 52 54] [71 73; 72 74]; [61 63; 62 64] [81 83; 82 84]]

dotplus(x,Ref(Ref(y)))
# 3-element Vector{Vector{Matrix{Int64}}}:
# [[51 73; 62 84], [51 73; 62 84]]
# [[51 73; 62 84], [51 73; 62 84]]
# [[51 73; 62 84], [51 73; 62 84]]

```

although you may need to change things depending on how exactly you want each term passed down through the broadcasting.

Broadcasting to significant depth is not terribly common.
