# What is the quickest way to move a subsection of a matrix?

**URL:** <https://discourse.julialang.org/t/what-is-the-quickest-way-to-move-a-subsection-of-a-matrix/18000>\
**Category:** General Usage\
**Tags:** matrices\
**Created:** [November 26, 2018, 2:58am UTC](https://discourse.julialang.org/t/what-is-the-quickest-way-to-move-a-subsection-of-a-matrix/18000 "2018-11-26T02:58:55Z")\
**Posts on this page:** 5\
**Page:** 2

<div class="post-metadata">

**Author:** ![pablosanjose](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pablosanjose/32/7006_2.png) [@pablosanjose](https://discourse.julialang.org/u/pablosanjose)\
**Post date:** [November 26, 2018, 3:06pm UTC](https://discourse.julialang.org/t/what-is-the-quickest-way-to-move-a-subsection-of-a-matrix/18000/21 "2018-11-26T15:06:46Z")

</div>

Phew! Heady stuff. So under that PR we would have `Base.mightalias(m, m) == false`? (I might be missing the point altogether, this is a bit too advanced for me :-D)

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [November 26, 2018, 5:22pm UTC](https://discourse.julialang.org/t/what-is-the-quickest-way-to-move-a-subsection-of-a-matrix/18000/22 "2018-11-26T17:22:16Z")

</div>

My main takeaway from all of this is “just use a for loop” 😄

---

<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:** [November 26, 2018, 9:01pm UTC](https://discourse.julialang.org/t/what-is-the-quickest-way-to-move-a-subsection-of-a-matrix/18000/23 "2018-11-26T21:01:21Z")

</div>

That’s very true for every piece of Julia code, but it’s quite frustrating when one is forced to write nested loops to keep performance or use weird `@views`, `copyto!`, `getindex`, etc., instead of a simple `m[:,1:2] = m[:,2:3]` as in Fortran or MATLAB. Sometimes when I port some Fortran code to Julia with a lot of operations on array slices and vectorized functions, I end up with a more complex Julia code to achieve the same performance. I’m not sure if this is a very tough problem, e.g. because of using a GC or it’s the design of dynamic languages in general, but I’m sure that Julia is improving day after day. Can I expect this to be solved in a near major release, or am I too optimistic?

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [November 26, 2018, 9:18pm UTC](https://discourse.julialang.org/t/what-is-the-quickest-way-to-move-a-subsection-of-a-matrix/18000/24 "2018-11-26T21:18:28Z")

</div>

I’m not saying that people should just use a for loop because it’s faster in this case—there are view-based solutions of utility functions that are just as efficient. In this case I just think a for loop is easier to understand. Otherwise I have to look up the `copyto!` function to know how it works.

---

<div class="post-metadata">

**Author:** ![pablosanjose](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pablosanjose/32/7006_2.png) [@pablosanjose](https://discourse.julialang.org/u/pablosanjose)\
**Post date:** [November 26, 2018, 10:26pm UTC](https://discourse.julialang.org/t/what-is-the-quickest-way-to-move-a-subsection-of-a-matrix/18000/25 "2018-11-26T22:26:49Z")

</div>

The way I see it Julia allows you to reason about what actually happens when you do `m[:,1:2] = m[:,2:3]`. It teaches you to _think_ about the possible pitfall of overwriting what you want to copy. Other languages will isolate you from these pitfalls by making a copy _always_, so that you don’t shoot yourself in the foot. In Julia you actually have different options, which allow you to achieve much higher performance. Actually you _can_ do `m[:,1:2] = m[:,2:3]` in Julia. It will make a copy, and work like in MATLAB or Mathematica. However Julians will not consider that an optimal choice simply because you have better, non-allocating options, as discussed here, unlike in many other high-level languages. Freedom is never bad!

[Previous page](https://discourse.julialang.org/t/what-is-the-quickest-way-to-move-a-subsection-of-a-matrix/18000.md?page=1)
