# Most efficient way to do \`\[maximum(x\[:,j\] .\* W) for j in 1:size(x, 2)\]\` where \`x\` is a matrix and \`W\` is a vector

**URL:** <https://discourse.julialang.org/t/most-efficient-way-to-do-maximum-x-j-w-for-j-in-1-size-x-2-where-x-is-a-matrix-and-w-is-a-vector/17804>\
**Category:** New to Julia\
**Tags:** flux\
**Created:** [November 21, 2018, 7:56am UTC](https://discourse.julialang.org/t/most-efficient-way-to-do-maximum-x-j-w-for-j-in-1-size-x-2-where-x-is-a-matrix-and-w-is-a-vector/17804 "2018-11-21T07:56:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [November 21, 2018, 7:56am UTC](https://discourse.julialang.org/t/most-efficient-way-to-do-maximum-x-j-w-for-j-in-1-size-x-2-where-x-is-a-matrix-and-w-is-a-vector/17804/1 "2018-11-21T07:56:28Z")

</div>

I have

```julia
W = rand(12)
x = rand(12, 1000)

```

and I would like multi each column of `x` by `W` element-wise then take the maximum of the column. I should end up with a vector with the same size as the number of coumns of `x`

The best way I can think of is

`[maximum(x[:,j] .* W) for j in 1:size(x, 2)]`

but I think there should a more efficient way

Actual, I wanted to do this in Flux.jl so the code is more like

```julia
# start random parameters
x = rand(12, 1000)
W = param([1 for i=1:12])
x1 = [maximum(x[:,j] .* W) for j in 1:size(x, 2)]

```

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [November 21, 2018, 8:12am UTC](https://discourse.julialang.org/t/most-efficient-way-to-do-maximum-x-j-w-for-j-in-1-size-x-2-where-x-is-a-matrix-and-w-is-a-vector/17804/2 "2018-11-21T08:12:39Z")

</div>

```julia
julia> using LazyArrays

julia> vec(maximum(BroadcastArray(*, W, x), dims=1));

```

---

<div class="post-metadata">

**Author:** ![Azamat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/azamat/32/6892_2.png) [@Azamat](https://discourse.julialang.org/u/Azamat)\
**Post date:** [November 21, 2018, 8:15am UTC](https://discourse.julialang.org/t/most-efficient-way-to-do-maximum-x-j-w-for-j-in-1-size-x-2-where-x-is-a-matrix-and-w-is-a-vector/17804/3 "2018-11-21T08:15:42Z")

</div>

There should be more efficient ways to do this, but I would do it as

```nohighlight
maximum(x .* W, dims=1)

```

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [November 21, 2018, 8:18am UTC](https://discourse.julialang.org/t/most-efficient-way-to-do-maximum-x-j-w-for-j-in-1-size-x-2-where-x-is-a-matrix-and-w-is-a-vector/17804/4 "2018-11-21T08:18:07Z")

</div>

`x .* W` allocates an intermediate array of the same shape as `x` which is not necessary, hence my `LazyArrays` solution which doesn’t materialize the array.

---

<div class="post-metadata">

**Author:** ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)\
**Post date:** [November 21, 2018, 8:33am UTC](https://discourse.julialang.org/t/most-efficient-way-to-do-maximum-x-j-w-for-j-in-1-size-x-2-where-x-is-a-matrix-and-w-is-a-vector/17804/5 "2018-11-21T08:33:07Z")

</div>

Benchmarks:

```julia
julia> @btime [maximum($(x)[:,j] .* $W) for j in 1:size($x, 2)];
  110.080 μs (2003 allocations: 351.75 KiB)

julia> @btime vec(maximum(BroadcastArray(*, $W, $x), dims=1));
  42.240 μs (15 allocations: 8.30 KiB)

julia> @btime @views [maximum($(x)[:,j] .* $W) for j in 1:size($x, 2)];
  75.093 μs (2003 allocations: 226.75 KiB)

```
