# Column wise broadcast for a matrix

**URL:** https://discourse.julialang.org/t/column-wise-broadcast-for-a-matrix/11113
**Category:** New to Julia
**Created:** [May 23, 2018, 10:10pm UTC](https://discourse.julialang.org/t/column-wise-broadcast-for-a-matrix/11113 "2018-05-23T22:10:08Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)
#### Post date: [May 23, 2018, 10:10pm UTC](https://discourse.julialang.org/t/column-wise-broadcast-for-a-matrix/11113/1 "2018-05-23T22:10:08Z")

</div>

Sorry if this has been posted a bunch but my googling has failed me. How do I broadcast a function by column for a 2 dimensional matrix? Say I want to, for each column in a matrix, normalize it with respect to the mean of that column. `normalize.(mymatrix)` will apply to each element, not each column.

---

<div class="post-metadata">

### Author: ![NiclasMattsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/niclasmattsson/32/21988_2.png) [@NiclasMattsson](https://discourse.julialang.org/u/NiclasMattsson)
#### Post date: [May 23, 2018, 10:44pm UTC](https://discourse.julialang.org/t/column-wise-broadcast-for-a-matrix/11113/2 "2018-05-23T22:44:17Z")

</div>

I think the most elegant way to broadcast a function by column is to use `mapslices`. So try this:

```julia
mapslices(normalize, mymatrix, 1)

```

The third argument specifies the dimensions to broadcast over. So 1 for columns, 2 for rows, or a vector for high level multidimensional magic. See the [documentation](https://docs.julialang.org/en/stable/stdlib/arrays/#Base.mapslices) for more info.

---

<div class="post-metadata">

### Author: ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)
#### Post date: [May 23, 2018, 10:45pm UTC](https://discourse.julialang.org/t/column-wise-broadcast-for-a-matrix/11113/3 "2018-05-23T22:45:20Z")

</div>

Thank you! exactly what I was looking for.

---

<div class="post-metadata">

### Author: ![rakeshvar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rakeshvar/32/3613_2.png) [@rakeshvar](https://discourse.julialang.org/u/rakeshvar)
#### Post date: [May 24, 2018, 8:34pm UTC](https://discourse.julialang.org/t/column-wise-broadcast-for-a-matrix/11113/4 "2018-05-24T20:34:37Z")

</div>

For more complicated things, one can use the do syntax I guess.

```julia
mapslices(mymatrix, 1) do x
x/norm(x)
end

```
