# Doubt regarding Broadcast assignment of 1d Arrays and Vectors

**URL:** <https://discourse.julialang.org/t/doubt-regarding-broadcast-assignment-of-1d-arrays-and-vectors/48189>\
**Category:** General Usage\
**Tags:** broadcast\
**Created:** [October 12, 2020, 2:04am UTC](https://discourse.julialang.org/t/doubt-regarding-broadcast-assignment-of-1d-arrays-and-vectors/48189 "2020-10-12T02:04:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sarthak\_Choudhury](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sarthak_choudhury/32/18593_2.png) [@Sarthak\_Choudhury](https://discourse.julialang.org/u/Sarthak_Choudhury)\
**Post date:** [October 12, 2020, 2:04am UTC](https://discourse.julialang.org/t/doubt-regarding-broadcast-assignment-of-1d-arrays-and-vectors/48189/1 "2020-10-12T02:04:15Z")

</div>

Hi,

I am new to Julia. I had a doubt regarding 1d arrays and vectors. If they represent the same thing then usually you can do algebraic functions on them but I found you cannot do assignment via broadcasting. A simple example would be with a 1 d array and a vector which are two ways of expressing the same quantity.

```julia
a=collect([1 2 3]')
b=[4;5;6]
#b.=a
c=b.+a

```

The addition works but not the commented out assignment. My question is why does the addition functionality work but not the assignment since Julia is quite clever to understand they are of same dimensions.

---

<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:** [October 12, 2020, 2:55am UTC](https://discourse.julialang.org/t/doubt-regarding-broadcast-assignment-of-1d-arrays-and-vectors/48189/2 "2020-10-12T02:55:01Z")

</div>

But your `a` has two dimensions – at least Julia thinks it does.

```julia
julia> a=collect([1 2 3]')
3×1 Array{Int64,2}:
 1
 2
 3

```

Obviously you can’t assign a two dimensional thing to a one dimensional thing in-place. So that’s why it doesn’t work.

Because there are only three elements in each, it will work if you just force it. Any of the following work fine.

```julia
b .= a[:]
b .= a[:,1]
b .= dropdims(a;dims=2)
b .= [a[i] for i in eachindex(a)]

```

along with many more.

---

<div class="post-metadata">

**Author:** ![Sarthak\_Choudhury](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sarthak_choudhury/32/18593_2.png) [@Sarthak\_Choudhury](https://discourse.julialang.org/u/Sarthak_Choudhury)\
**Post date:** [October 12, 2020, 3:08am UTC](https://discourse.julialang.org/t/doubt-regarding-broadcast-assignment-of-1d-arrays-and-vectors/48189/3 "2020-10-12T03:08:52Z")

</div>

Oh I agree. The opposite does not work either. i.e if you want to have

```julia
a.=b

```

I am amazed that Julia “magically” knows how to add the two together since they are different types and return a 2d array. I was just wondering why if it could add them together it could not assign one to the other elementwise. I am a newbie so this might be a stupid question 😛.

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [October 12, 2020, 3:16am UTC](https://discourse.julialang.org/t/doubt-regarding-broadcast-assignment-of-1d-arrays-and-vectors/48189/4 "2020-10-12T03:16:00Z")

</div>

I think the reason this happens is that the dimension check is tighter for assignment than all other operations. Since `[1 2 3; 4 5 6] .+ [1; 2]` works (expanding the 1 dimension result along the longer axis) the operation is defined for objects of different dimensions. For `.=`, however you have to store data so you can’t automatically expand along dimensions. As such, no equivilent method is defined to allow `.=` for vectors and matrices

---

<div class="post-metadata">

**Author:** ![Sarthak\_Choudhury](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sarthak_choudhury/32/18593_2.png) [@Sarthak\_Choudhury](https://discourse.julialang.org/u/Sarthak_Choudhury)\
**Post date:** [October 12, 2020, 3:22am UTC](https://discourse.julialang.org/t/doubt-regarding-broadcast-assignment-of-1d-arrays-and-vectors/48189/5 "2020-10-12T03:22:23Z")

</div>

Thanks a lot for your answers.
