# Operation on columns of a matrix

**URL:** <https://discourse.julialang.org/t/operation-on-columns-of-a-matrix/50060>\
**Category:** Performance\
**Tags:** matrices\
**Created:** [November 12, 2020, 5:16pm UTC](https://discourse.julialang.org/t/operation-on-columns-of-a-matrix/50060 "2020-11-12T17:16:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![nhavt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nhavt/32/16356_2.png) [@nhavt](https://discourse.julialang.org/u/nhavt)\
**Post date:** [November 12, 2020, 5:16pm UTC](https://discourse.julialang.org/t/operation-on-columns-of-a-matrix/50060/1 "2020-11-12T17:16:30Z")

</div>

Hello there,

I would like to ask a question regarding an operation on two columns of a matrix as you can see below. Here I proposed two functions to perform the task. The function f1 has two allocations even though I used @view, while the function f2 has no allocation. I am wondering whether it is possible to perform the task with no allocations without using a loop as proposed in function f2? Thanks in advance!

```julia
f1(x,A,iA,iB) = x .= view(A,:,iA) - view(A,:,iB);
function f2(x,A,iA,iB)
    @inbounds @simd for i in 1:size(A,1)
        x[i] = A[i,iA] - A[i,iB]
    end
end
A = rand(10_000,10_000);
x1 = rand(10_000);
x2 = rand(10_000);
x0 = A[:,6] - A[:,8];
@time f1(x1,A,6,8);
@time f2(x2,A,6,8);
@test x0 ≈ x1
@test x0 ≈ x2

```

---

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [November 12, 2020, 5:28pm UTC](https://discourse.julialang.org/t/operation-on-columns-of-a-matrix/50060/2 "2020-11-12T17:28:19Z")

</div>

The allocation is occurring on the right hand side of th assignment because you have `-` instead of `.-`

```julia
julia> f3(x,A,iA,iB) = x .= view(A,:,iA) .- view(A,:,iB);

julia> begin
           x1 = rand(10_000);
           x2 = rand(10_000);
           x3 = rand(10_000);
           x0 = A[:,6] - A[:,8];
           @btime f1($x1, $A, 6, 8);
           @btime f2($x2, $A, 6, 8);
           @btime f3($x3, $A, 6, 8);
       end
  13.495 μs (2 allocations: 78.20 KiB)
  4.723 μs (0 allocations: 0 bytes)
  5.556 μs (0 allocations: 0 bytes)
```

---

<div class="post-metadata">

**Author:** ![moeddel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/moeddel/32/18641_2.png) [@moeddel](https://discourse.julialang.org/u/moeddel)\
**Post date:** [November 13, 2020, 7:50am UTC](https://discourse.julialang.org/t/operation-on-columns-of-a-matrix/50060/3 "2020-11-13T07:50:26Z")

</div>

> [@nhavt](#):
>
> I am wondering whether it is possible to perform the task with no allocations without using a loop as proposed in function f2? Thanks in advance!

I am wondering, why you want to achieve this?

---

<div class="post-metadata">

**Author:** ![nhavt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nhavt/32/16356_2.png) [@nhavt](https://discourse.julialang.org/u/nhavt)\
**Post date:** [November 13, 2020, 7:51am UTC](https://discourse.julialang.org/t/operation-on-columns-of-a-matrix/50060/4 "2020-11-13T07:51:45Z")

</div>

> The allocation is occurring on the right hand side of th assignment because you have `-` instead of `.-`

Indeed, you were right! I didn’t check it carefully. Thanks for your help!
