# Doubt regarding Broadcast assignment of 1d Arrays and Vectors

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.

``````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.

2 Likes

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

``````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.

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

along with many more.

2 Likes

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

``````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 .

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

4 Likes