# One-dimensional matrix and vectors in broadcast

**URL:** <https://discourse.julialang.org/t/one-dimensional-matrix-and-vectors-in-broadcast/56146>\
**Category:** Internals & Design\
**Created:** [February 27, 2021, 5:07pm UTC](https://discourse.julialang.org/t/one-dimensional-matrix-and-vectors-in-broadcast/56146 "2021-02-27T17:07:45Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [February 27, 2021, 5:07pm UTC](https://discourse.julialang.org/t/one-dimensional-matrix-and-vectors-in-broadcast/56146/1 "2021-02-27T17:07:45Z")

</div>

In a [recent thread](https://discourse.julialang.org/t/python-vs-julia-ode-solver/56123/22) I noticed that mutating a vector by broadcasting operations on one-dimensional matrices does not work:

```julia
julia> x = rand(3,1);

julia> z = zeros(3);

julia> z .= x 
ERROR: DimensionMismatch("cannot broadcast array to have fewer dimensions")

```

On the other side, the symmetric mutation does work:

```julia
julia> x .= z
3×1 Array{Float64,2}:
 0.0
 0.0
 0.0

```

Looks like something that could be improved from a user perspective. Is there any design reason for this, an implementation difficulty, or what?

I found [some discussions](https://discourse.julialang.org/t/consistency-of-eye-vs-zeros-ones-rand-functions/2011/11) related to this, but perhaps there is a more clear view on the subject now.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [February 28, 2021, 10:08am UTC](https://discourse.julialang.org/t/one-dimensional-matrix-and-vectors-in-broadcast/56146/2 "2021-02-28T10:08:26Z")

</div>

> [@lmiq](#):
>
> something that could be improved from a user perspective

What do you have in mind?

> [@lmiq](#):
>
> Is there any design reason for this, an implementation difficulty, or what?

Julia’s broadcasting mechanism generally rejects incompatible dimensions (in contrast to, say, R’s “recycling”). See, in particular, [`?broadcast`](https://docs.julialang.org/en/v1/base/arrays/#Base.Broadcast.broadcast), which tells you that

> Singleton and missing dimensions are expanded to match the extents of the other arguments by virtually repeating the value.

This is what’s happening in `x .= z` above.

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [February 28, 2021, 10:17am UTC](https://discourse.julialang.org/t/one-dimensional-matrix-and-vectors-in-broadcast/56146/3 "2021-02-28T10:17:25Z")

</div>

Yes, I think it would not be unreasonable to allow this. Feel free to open an issue on GitHub.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [February 28, 2021, 2:11pm UTC](https://discourse.julialang.org/t/one-dimensional-matrix-and-vectors-in-broadcast/56146/4 "2021-02-28T14:11:30Z")

</div>

> [@Tamas\_Papp](#):
>
> What do you have in mind?

From a user perspective just that it worked. I tried to follow the function call, but the way broadcasting is implemented is way too complicated for me.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [March 3, 2021, 5:44pm UTC](https://discourse.julialang.org/t/one-dimensional-matrix-and-vectors-in-broadcast/56146/5 "2021-03-03T17:44:35Z")

</div>

Done: [broadcast (Nx1)-dimensional array to vector does not work · Issue #39904 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/39904)

---

<div class="post-metadata">

**Author:** ![apo383](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/apo383/32/11272_2.png) [@apo383](https://discourse.julialang.org/u/apo383)\
**Post date:** [March 4, 2021, 12:55am UTC](https://discourse.julialang.org/t/one-dimensional-matrix-and-vectors-in-broadcast/56146/6 "2021-03-04T00:55:48Z")

</div>

> [@lmiq](#):
>
> broadcast one-dimensional array to vector

I don’t disagree with your issue, but to nitpick: Julia would normally call `x` a two-dimensional array `3×1 Array{Float64,2}`, where one of the dimensions is 1, to be distinguished from one-dimensional `Vector`. Of course, Matlab treats them as the same, but Julia doesn’t. Not sure what they should be called here, maybe 1-column matrix?

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [March 4, 2021, 11:57am UTC](https://discourse.julialang.org/t/one-dimensional-matrix-and-vectors-in-broadcast/56146/7 "2021-03-04T11:57:11Z")

</div>

Well, that was quick: [allow trailing dims in inplace broadcast by simeonschaub · Pull Request #39859 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/pull/39859), thank you @simeonschaub

---

<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 4, 2021, 5:20pm UTC](https://discourse.julialang.org/t/one-dimensional-matrix-and-vectors-in-broadcast/56146/8 "2021-03-04T17:20:16Z")

</div>

Gotta love it when the patch precedes the issue 😛
