# Broadcasting across columns of a matrix

**URL:** https://discourse.julialang.org/t/broadcasting-across-columns-of-a-matrix/18496
**Category:** General Usage
**Created:** [December 9, 2018, 6:11pm UTC](https://discourse.julialang.org/t/broadcasting-across-columns-of-a-matrix/18496 "2018-12-09T18:11:11Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![vgupta1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vgupta1/32/6381_2.png) [@vgupta1](https://discourse.julialang.org/u/vgupta1)
#### Post date: [December 9, 2018, 6:11pm UTC](https://discourse.julialang.org/t/broadcasting-across-columns-of-a-matrix/18496/1 "2018-12-09T18:11:11Z")

</div>

I am struggling to use the dot operator/ broadcasting correctly though when I’d like to broadcast across columns of a matrix.

Suppose I have a function:

```julia
function myfun(col_vec, scalar)
   #does something complex operation 
   ...
   return output
end

```

I’d like a ``vector" version that operates on a matrix by column:

```julia
function myfun_vec(mat, vec)
   out = length(output)
   for ix = 1:length(vec)
      out[ix] = myfun(mat[:, ix], vec[ix])
   end
   out
end

```

Can I use dot syntax / broadcasting to avoid implementing myfun\_vec instead? Note

```julia
   myfun. ( mat, vec)  

```

does not return what I want. I mostly want the “.” version so that I can write things like:

```julia
myfun. (mat, 2) #should return myfun_vec(mat, 2 * ones(dim(mat, 2) )

```

If redefine the data structure of mat to be a vector of vectors and then everything works fine, but that seems super cludgey and doesn’t match surrounding code.

---

<div class="post-metadata">

### Author: ![zekeriya.sari](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zekeriya.sari/32/13695_2.png) [@zekeriya.sari](https://discourse.julialang.org/u/zekeriya.sari)
#### Post date: [December 9, 2018, 7:53pm UTC](https://discourse.julialang.org/t/broadcasting-across-columns-of-a-matrix/18496/2 "2018-12-09T19:53:08Z")

</div>

Here is my try,

```julia
julia> myfun(col_vec, scalar) = scalar * col_vec
myfun (generic function with 1 method)

julia> mat = collect(reshape(1:10, 2, 5))
2×5 Array{Int64,2}:
 1 3 5 7 9
 2 4 6 8 10

julia> hcat(myfun.([mat[:, i] for i = 1 : size(mat, 2)], collect(1:size(mat, 2)))...)
2×5 Array{Int64,2}:
 1 6 15 28 45
 2 8 18 32 50

```

I think that in order to use the syntax, `myfun.(arg1, arg2)`, the collections `arg1` and `arg2` must have the same length.

---

<div class="post-metadata">

### Author: ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)
#### Post date: [December 9, 2018, 8:33pm UTC](https://discourse.julialang.org/t/broadcasting-across-columns-of-a-matrix/18496/3 "2018-12-09T20:33:28Z")

</div>

One option if you know the number of rows of your matrix is to reinterpret it as a vector of SVectors from StaticArrays.jl. This doesn’t copy any data and works very nicely if you have a small number of rows:

```julia
julia> A = rand(2, 5)
2×5 Array{Float64,2}:
 0.969289 0.743624 0.280098 0.401384 0.0525567
 0.765344 0.951552 0.913606 0.208708 0.136869 

julia> using StaticArrays

julia> v = reinterpret(SVector{2, Float64}, A);

julia> println.(v)
[0.969289, 0.765344]
[0.743624, 0.951552]
[0.280098, 0.913606]
[0.401384, 0.208708]
[0.0525567, 0.136869]

```

---

<div class="post-metadata">

### Author: ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)
#### Post date: [December 9, 2018, 10:03pm UTC](https://discourse.julialang.org/t/broadcasting-across-columns-of-a-matrix/18496/4 "2018-12-09T22:03:21Z")

</div>

You can try:

```julia
myfun.( [view(mat,:,i) for i in 1:size(mat,2)], vec)

```

Still, `views` are allocating but not too much.

---

<div class="post-metadata">

### Author: ![Mattriks](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mattriks/32/351_2.png) [@Mattriks](https://discourse.julialang.org/u/Mattriks)
#### Post date: [December 10, 2018, 9:14am UTC](https://discourse.julialang.org/t/broadcasting-across-columns-of-a-matrix/18496/6 "2018-12-10T09:14:28Z")

</div>

Here are some alternatives using `mapslices`. Both assume that you have a function `myfun(col_vec, scalar)` as above. Note ii) adds an additional method to `myfun()`.

Cases:  
i) The scalars in `vec` are the same:

```julia
mapslices(x->myfun(x,2), mat, dims=1)

```

ii) The scalars in `vec` are the same or different e.g. `vec = reshape(rand(3), 1,3)`

```julia
myfun(col_vec) = myfun(col_vec[2:end], col_vec[1])
mapslices(myfun, vcat(vec,mat), dims=1)

```
