# Accessing rows without copying

**URL:** https://discourse.julialang.org/t/accessing-rows-without-copying/49945
**Category:** Performance
**Created:** [November 10, 2020, 8:49pm UTC](https://discourse.julialang.org/t/accessing-rows-without-copying/49945 "2020-11-10T20:49:10Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![barthelemymp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/barthelemymp/32/23687_2.png) [@barthelemymp](https://discourse.julialang.org/u/barthelemymp)
#### Post date: [November 10, 2020, 8:49pm UTC](https://discourse.julialang.org/t/accessing-rows-without-copying/49945/1 "2020-11-10T20:49:10Z")

</div>

Hello,

I am optimizing my code, and I wanted to know how I could access the row of a matrix directly (not making a copy of it, and pass it to a function.  
right now I do:

```julia
@threads for i=1:n_rows
      results[i] = myfunction(MyMatrix[i,:])
end

```

However I was told that MyMatrix[i,:] is making a copy of the row.  
How could I give to myfunction the original row ? The only solution I found yes is to rewrite myfunction(MyMatrix, m) so that it take the complete array and the row index… I think there must a more efficient solution…

Best

---

<div class="post-metadata">

### Author: ![Vasily\_Pisarev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vasily_pisarev/32/7929_2.png) [@Vasily\_Pisarev](https://discourse.julialang.org/u/Vasily_Pisarev)
#### Post date: [November 10, 2020, 9:01pm UTC](https://discourse.julialang.org/t/accessing-rows-without-copying/49945/2 "2020-11-10T21:01:46Z")

</div>

`view(M, i, :)` or, for convenience, `@view M[i, :]` creates a “view” into the array.

Keep in mind that Julia uses column-major storage, meaning that elements in a row (or a row view) are non-contiguous in memory. That means, working on a copy which has the same elements stored contiguously may happen to be faster despite the need for an extra allocation.

If possible, it’s better to rewrite your program so that the relevant data are stored in rows of `MyMatrix`.
