# Columnwise/rowwise elementwise multiplication?

**URL:** <https://discourse.julialang.org/t/columnwise-rowwise-elementwise-multiplication/6412>\
**Category:** General Usage\
**Tags:** array\
**Created:** [October 12, 2017, 3:12pm UTC](https://discourse.julialang.org/t/columnwise-rowwise-elementwise-multiplication/6412 "2017-10-12T15:12:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Daneel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/daneel/32/175_2.png) [@Daneel](https://discourse.julialang.org/u/Daneel)\
**Post date:** [October 12, 2017, 3:12pm UTC](https://discourse.julialang.org/t/columnwise-rowwise-elementwise-multiplication/6412/1 "2017-10-12T15:12:17Z")

</div>

Is there an easier way of element-wise multiplying a vector, x, by each column in an array? Below I do it with a `for` loop. I’ve tried using mapslices but I still need to broadcast x over each slice.

```julia
julia> x = [1;2;3]
3-element Array{Int64,1}:
 1
 2
 3

julia> y = [1 2 3 4 5;6 7 8 9 10;11 12 13 14 15]
3×5 Array{Int64,2}:
  1 2 3 4 5
  6 7 8 9 10
 11 12 13 14 15

julia> z = zeros(3,5)
3×5 Array{Float64,2}:
 0.0 0.0 0.0 0.0 0.0
 0.0 0.0 0.0 0.0 0.0
 0.0 0.0 0.0 0.0 0.0

julia> for i=1:size(y,2);z[:,i] = x.*y[:,i];end

julia> z
3×5 Array{Float64,2}:
  1.0 2.0 3.0 4.0 5.0
 12.0 14.0 16.0 18.0 20.0
 33.0 36.0 39.0 42.0 45.0

```

---

<div class="post-metadata">

**Author:** ![Evizero](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/evizero/32/10118_2.png) [@Evizero](https://discourse.julialang.org/u/Evizero)\
**Post date:** [October 12, 2017, 3:31pm UTC](https://discourse.julialang.org/t/columnwise-rowwise-elementwise-multiplication/6412/2 "2017-10-12T15:31:26Z")

</div>

```julia
julia> x.*y
3×5 Array{Int64,2}:
  1 2 3 4 5
 12 14 16 18 20
 33 36 39 42 45

julia> Float64.(x.*y)
3×5 Array{Float64,2}:
  1.0 2.0 3.0 4.0 5.0
 12.0 14.0 16.0 18.0 20.0
 33.0 36.0 39.0 42.0 45.0

```

---

<div class="post-metadata">

**Author:** ![Daneel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/daneel/32/175_2.png) [@Daneel](https://discourse.julialang.org/u/Daneel)\
**Post date:** [October 13, 2017, 6:13am UTC](https://discourse.julialang.org/t/columnwise-rowwise-elementwise-multiplication/6412/3 "2017-10-13T06:13:50Z")

</div>

🤦‍♂️

How I missed that I have no idea. Thanks.
