# Generic way to apply function to a dimension of a matrix

**URL:** <https://discourse.julialang.org/t/generic-way-to-apply-function-to-a-dimension-of-a-matrix/54378>\
**Category:** New to Julia\
**Tags:** array\
**Created:** [February 1, 2021, 2:35pm UTC](https://discourse.julialang.org/t/generic-way-to-apply-function-to-a-dimension-of-a-matrix/54378 "2021-02-01T14:35:10Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ribeiro](https://avatars.discourse-cdn.com/v4/letter/r/d9b06d/32.png) [@Ribeiro](https://discourse.julialang.org/u/Ribeiro)\
**Post date:** [February 1, 2021, 2:35pm UTC](https://discourse.julialang.org/t/generic-way-to-apply-function-to-a-dimension-of-a-matrix/54378/1 "2021-02-01T14:35:10Z")

</div>

Hello!  
The mean function from statistics has the “dims” option, which lets me average a matrix into a vector by averaging the rows or columns.  
Is there a generic way to do this without for loops? Say I want to do the same thing with the dot function, where I have a 1x3 array and a 10x3 matrix and I want to do the dot product of the array by each line of the matrix. Using dot.(a,b) wouldn’t really work as Julia doesn’t know which dims to use.  
Thanks!

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [February 1, 2021, 2:42pm UTC](https://discourse.julialang.org/t/generic-way-to-apply-function-to-a-dimension-of-a-matrix/54378/2 "2021-02-01T14:42:52Z")

</div>

> [@Ribeiro](#):
>
> I have a 1x3 array and a 10x3 matrix and I want to do the dot product of the array by each line of the matrix

This particular computation is exactly a matrix product, so just doing `M * v'` will be the best approach.

More generally, you can use the `eachslice`, `eachrow`, or `eachcol` iterators.

> Is there a generic way to do this without for loops?

Note also that there’s nothing wrong with a loop in Julia. Loops are fast, and are sometimes the simplest and clearest way to code something.

---

<div class="post-metadata">

**Author:** ![Ribeiro](https://avatars.discourse-cdn.com/v4/letter/r/d9b06d/32.png) [@Ribeiro](https://discourse.julialang.org/u/Ribeiro)\
**Post date:** [February 1, 2021, 3:24pm UTC](https://discourse.julialang.org/t/generic-way-to-apply-function-to-a-dimension-of-a-matrix/54378/3 "2021-02-01T15:24:12Z")

</div>

Thanks, Steven.  
You’re right, bad example. Say I want the norm of the 10x3 matrix as a 10 element array (i.e., norm of each line).  
I’d like to know how to do it without for loops to be able to easily use CUDA.  
Thanks!

---

<div class="post-metadata">

**Author:** ![Ribeiro](https://avatars.discourse-cdn.com/v4/letter/r/d9b06d/32.png) [@Ribeiro](https://discourse.julialang.org/u/Ribeiro)\
**Post date:** [February 1, 2021, 3:33pm UTC](https://discourse.julialang.org/t/generic-way-to-apply-function-to-a-dimension-of-a-matrix/54378/4 "2021-02-01T15:33:26Z")

</div>

Yep, combining eachrow with the . (say norm.(eachrow(matrix))) worked! Thanks!

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [February 1, 2021, 4:20pm UTC](https://discourse.julialang.org/t/generic-way-to-apply-function-to-a-dimension-of-a-matrix/54378/5 "2021-02-01T16:20:56Z")

</div>

> [@Ribeiro](#):
>
> `norm.(eachrow(matrix)))`

It is a bit more efficient to do `map(norm, eachrow(matrix))` or `[norm(v) for v in eachrow(matrix)]` instead of `norm.(eachrow(matrix))` here — with the dot syntax, if you apply it to an iterator like `eachrow(matrix)` then it first runs `collect` (to form an explicit array of the row views).

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [February 1, 2021, 7:00pm UTC](https://discourse.julialang.org/t/generic-way-to-apply-function-to-a-dimension-of-a-matrix/54378/6 "2021-02-01T19:00:04Z")

</div>

Just be aware that `eachrow` iterate over `Vector`s not `Arrays{T, 2}` of dimensions `1xN`, [this confused a colleague of mine when he started using Julia](https://discourse.julialang.org/t/eachrow-over-array-any-2-does-not-return-rows-why/34601).

---

<div class="post-metadata">

**Author:** ![Ribeiro](https://avatars.discourse-cdn.com/v4/letter/r/d9b06d/32.png) [@Ribeiro](https://discourse.julialang.org/u/Ribeiro)\
**Post date:** [February 1, 2021, 8:01pm UTC](https://discourse.julialang.org/t/generic-way-to-apply-function-to-a-dimension-of-a-matrix/54378/7 "2021-02-01T20:01:43Z")

</div>

Thanks, guys. This is all very helpful.  
I tested `norm.(eachrow(matrix))` and `map(norm, eachrow(matrix))` and the latter is indeed much more efficient in terms of number of allocations (measured with `@time`).  
Thanks again!
