# Broadcasting zip

**URL:** <https://discourse.julialang.org/t/broadcasting-zip/9229>\
**Category:** General Usage\
**Tags:** question\
**Created:** [February 21, 2018, 12:03pm UTC](https://discourse.julialang.org/t/broadcasting-zip/9229 "2018-02-21T12:03:02Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [February 21, 2018, 12:03pm UTC](https://discourse.julialang.org/t/broadcasting-zip/9229/1 "2018-02-21T12:03:03Z")

</div>

Say I have one column vector `x` with `n` elements in it, and one row vector `y` with `m` elements in it.  
If I want to zip the _broadcast_ of these two vectors, `zip(x .+ 0*y, 0*x .+ y)`, can I do it **without** generating these temporary `n × m` arrays?

Can I create `z` without creating `tempx` and `tempy`:

```julia
x = rand(3)'
y = rand(5)
tempx = x .+ 0*y
tempy = 0*x .+ y
z = zip(tempx, tempy)

```

---

<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 21, 2018, 12:16pm UTC](https://discourse.julialang.org/t/broadcasting-zip/9229/2 "2018-02-21T12:16:16Z")

</div>

```julia
julia> x = randn(10);

julia> y = randn(10);

julia> z1 = zip(x .+ 0*y, 0*x .+ y);

julia> z2 = ((x + 0*y, 0*x + y) for (x,y) in zip(x, y));

julia> collect(z1) == collect(z2)
true

```

---

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [February 21, 2018, 12:23pm UTC](https://discourse.julialang.org/t/broadcasting-zip/9229/3 "2018-02-21T12:23:50Z")

</div>

See the MWE above, I think I/you might have misunderstood something.

---

<div class="post-metadata">

**Author:** ![y4lu](https://avatars.discourse-cdn.com/v4/letter/y/47e85d/32.png) [@y4lu](https://discourse.julialang.org/u/y4lu)\
**Post date:** [February 21, 2018, 12:56pm UTC](https://discourse.julialang.org/t/broadcasting-zip/9229/4 "2018-02-21T12:56:02Z")

</div>

```julia
x = rand(3)'; 
y = rand(5);
z = broadcast((x,y)->(x,y), x, y);
z2 = zip(z);

```

---

<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 21, 2018, 2:15pm UTC](https://discourse.julialang.org/t/broadcasting-zip/9229/5 "2018-02-21T14:15:20Z")

</div>

Sorry, I missed the `'`. However, I still don’t get the example, because I don’t understand why you are not using just

```julia
tuple.(x, y)

```

---

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [February 21, 2018, 2:45pm UTC](https://discourse.julialang.org/t/broadcasting-zip/9229/6 "2018-02-21T14:45:34Z")

</div>

> [@Tamas\_Papp](#):
>
> tuple.(x, y)

Well, aren’t you fancy! 😁

OK, I’ll come clean, I have 3 things:

1. a matrix of weights
2. a range indicating the rows of that matrix
3. a range indicating the columns of that matrix

I want to calculate the weighted mean of the locations, and I thought I’d do it in the most efficient way possible. The ideas here lead to this form:

```julia
rows = 3:5 # in reality the locations of the rows can also be `linspace(Float64, Float64, n)` 
cols = -2:3
w = rand(length(rows), length(cols)) # I don't actually generate this matrix like that
S = sum(w)
r = sum(w[i]*rows[i[1]] for i in CartesianRange(indices(w)))/S
c = sum(w[i]*cols[i[1]] for i in CartesianRange(indices(w)))/S

```

---

<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 21, 2018, 6:08pm UTC](https://discourse.julialang.org/t/broadcasting-zip/9229/7 "2018-02-21T18:08:43Z")

</div>

> [@yakir12](#):
>
> the most efficient way possible

These kind of calculations should be

1. very fast anyway for small matrices,
2. depend on the memory layout for large matrices.

If this is _not_ a bottleneck worth optimizing (my standard assumption these days), I would go for readability. I think that `StatsBase` already has a method for weighted means, which may work for you. Otherwise clever use of broadcasting gives you one-liners.

If this _is_ a bottleneck, I would consider transposing for efficient traversal of rows, and benchmark.

---

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [February 21, 2018, 10:26pm UTC](https://discourse.julialang.org/t/broadcasting-zip/9229/8 "2018-02-21T22:26:26Z")

</div>

> [@Tamas\_Papp](#):
>
> this is not a bottleneck

Thanks for the perspective. I’ve been suffering from an acute case of premature over optimizing lately…
