# Sum over arrays - What am I doing wrong?

**URL:** <https://discourse.julialang.org/t/sum-over-arrays-what-am-i-doing-wrong/55831>\
**Category:** General Usage\
**Tags:** question, broadcast, array\
**Created:** [February 23, 2021, 8:39am UTC](https://discourse.julialang.org/t/sum-over-arrays-what-am-i-doing-wrong/55831 "2021-02-23T08:39:36Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![SubhadityaMukherjee](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/subhadityamukherjee/32/15983_2.png) [@SubhadityaMukherjee](https://discourse.julialang.org/u/SubhadityaMukherjee)\
**Post date:** [February 23, 2021, 8:39am UTC](https://discourse.julialang.org/t/sum-over-arrays-what-am-i-doing-wrong/55831/1 "2021-02-23T08:39:37Z")

</div>

Im trying to port a python numpy implementation of something into Julia and one step is

```nohighlight
import numpy as np
np.random.rand(12,10) + np.random.rand(10)

```

Output shape - (12, 10)  
but in Julia

```julia
rand(12,10) + rand(10)

```

throws DimensionMismatch(“dimensions must match: a has dims (Base.OneTo(12), Base.OneTo(10)), b has dims (Base.OneTo(10),), mismatch at 1”)

I tried broadcasting

```julia
rand(12,10) .+ rand(10)

```

DimensionMismatch(“arrays could not be broadcast to a common size; got a dimension with lengths 12 and 10”)

Im **so** confused. Is there something different here?

---

<div class="post-metadata">

**Author:** ![Geoffrey](https://avatars.discourse-cdn.com/v4/letter/g/bc8723/32.png) [@Geoffrey](https://discourse.julialang.org/u/Geoffrey)\
**Post date:** [February 23, 2021, 9:07am UTC](https://discourse.julialang.org/t/sum-over-arrays-what-am-i-doing-wrong/55831/2 "2021-02-23T09:07:45Z")

</div>

I’m not sure what ‘+’ does in python but if you want to add rand(10) to each rows of rand(12, 10) you should store them as a temporary array then add:

```julia
tmp = rand(10)
tmp2 = rand(12, 10)
vcat([permutedims(tmp2[i, :] + tmp) for i in 1:12]...)

```

Youd need the `permutedims` to keep row array when indexing in it and the `vcat` to concatenate all the rows.

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [February 23, 2021, 9:28am UTC](https://discourse.julialang.org/t/sum-over-arrays-what-am-i-doing-wrong/55831/3 "2021-02-23T09:28:20Z")

</div>

Broadcasting in Julia can expand dimensions that have only one value, like in Python, but it has to be the “right” dimension.

Here you are adding a 10-value array to a matrix with 10 columns and 12 rows. That will work if the array is a _row_ so it can be expanded to 12 rows:

```julia
rand(12,10) .+ rand(10)'

```

---

<div class="post-metadata">

**Author:** ![SubhadityaMukherjee](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/subhadityamukherjee/32/15983_2.png) [@SubhadityaMukherjee](https://discourse.julialang.org/u/SubhadityaMukherjee)\
**Post date:** [February 23, 2021, 9:30am UTC](https://discourse.julialang.org/t/sum-over-arrays-what-am-i-doing-wrong/55831/4 "2021-02-23T09:30:27Z")

</div>

Ohh okay that makes sense

---

<div class="post-metadata">

**Author:** ![SubhadityaMukherjee](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/subhadityamukherjee/32/15983_2.png) [@SubhadityaMukherjee](https://discourse.julialang.org/u/SubhadityaMukherjee)\
**Post date:** [February 23, 2021, 9:30am UTC](https://discourse.julialang.org/t/sum-over-arrays-what-am-i-doing-wrong/55831/5 "2021-02-23T09:30:46Z")

</div>

Okay now I get it. I thought it would be a direct translation.  
Thanks a lot!

---

<div class="post-metadata">

**Author:** ![gbaraldi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gbaraldi/32/22101_2.png) [@gbaraldi](https://discourse.julialang.org/u/gbaraldi)\
**Post date:** [February 23, 2021, 9:39am UTC](https://discourse.julialang.org/t/sum-over-arrays-what-am-i-doing-wrong/55831/6 "2021-02-23T09:39:14Z")

</div>

The reason for this difference is that while python creates row vectors when calling np.random.rand, julia creates a column vector. Which explains your dimension mismatch.

---

<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:** [February 24, 2021, 6:37am UTC](https://discourse.julialang.org/t/sum-over-arrays-what-am-i-doing-wrong/55831/7 "2021-02-24T06:37:22Z")

</div>

I think it’s easier to remember that in Python, you have to start aligning dimensions from the right, and in Julia from the left. That’s because Python is row-major and therefore the right-most dimension is the one contiguous in memory, while in Julia it’s the left-most.

In this example, broadcasting only works if two numbers on top of each other are the same, or at least one of them is `1`. The `[1]` are implicit singleton dimensions that will always match, if one of the arguments is shorter than another.

```julia
# python: align from the right
arr1: 12 10
arr2: [1] 10

# julia: align from the left, 12 and 10 are incompatible
arr1: 12 10
arr2: 10 [1]

# julia: with rand(10)', we have added a singleton dimension
# to arr2, so now it works
arr1: 12 10
arr2: 1 10

```

That works in higher dimensions, too, where it’s not just about rows and columns anymore.

I actually think it would be cool if the error messages for broadcasting gave such a visualization, but maybe it would be too crazy with many components.
