# 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:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Raf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raf/32/3383_2.png) [@Raf](https://discourse.julialang.org/u/Raf)\
**Post date:** [November 26, 2018, 2:58am UTC](https://discourse.julialang.org/t/what-is-the-quickest-way-to-move-a-subsection-of-a-matrix/18000/1 "2018-11-26T02:58:55Z")

</div>

For example copy the second two columns to the first:

```julia
[1 2 3;
 1 2 3;
 1 2 3]

```

becomes

```julia
[2 3 3;
 2 3 3;
 2 3 3]

```

---

<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, 7:50am UTC](https://discourse.julialang.org/t/what-is-the-quickest-way-to-move-a-subsection-of-a-matrix/18000/2 "2018-11-26T07:50:12Z")

</div>

How about `m[:, 1:2] .= view(m, :, 2:3)`?

---

<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, 7:58am UTC](https://discourse.julialang.org/t/what-is-the-quickest-way-to-move-a-subsection-of-a-matrix/18000/3 "2018-11-26T07:58:24Z")

</div>

Correction: `copyto!(m, CartesianIndices((1:3, 1:2)), m, CartesianIndices((1:3,2:3)))` is faster:

```julia
julia> @btime copyto!($m, CartesianIndices((1:3, 1:2)), $m, CartesianIndices((1:3,2:3)));
  39.121 ns (1 allocation: 160 bytes)

julia> f(m) = m[:,1:2] .= view(m, :, 2:3); @btime f($m);
  68.940 ns (4 allocations: 320 bytes)

```

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [November 26, 2018, 7:59am UTC](https://discourse.julialang.org/t/what-is-the-quickest-way-to-move-a-subsection-of-a-matrix/18000/4 "2018-11-26T07:59:57Z")

</div>

It is never clear to me whether the two regions of `copyto!` or `.=` can overlap, so I generally avoid this.

---

<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, 8:02am UTC](https://discourse.julialang.org/t/what-is-the-quickest-way-to-move-a-subsection-of-a-matrix/18000/5 "2018-11-26T08:02:06Z")

</div>

Interesting! I believe `copyto!` takes care of unaliasing source and destination. Can you give an example where overlaps are a problem?

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [November 26, 2018, 8:22am UTC](https://discourse.julialang.org/t/what-is-the-quickest-way-to-move-a-subsection-of-a-matrix/18000/6 "2018-11-26T08:22:33Z")

</div>

I think you misunderstand: since I don’t see this documented, this is something I don’t think I can rely on. Whether this works or not is not relevant, since if this is not part of the interface then it could silently stop working or introduce incorrect results.

---

<div class="post-metadata">

**Author:** ![bennedich](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bennedich/32/4894_2.png) [@bennedich](https://discourse.julialang.org/u/bennedich)\
**Post date:** [November 26, 2018, 8:43am UTC](https://discourse.julialang.org/t/what-is-the-quickest-way-to-move-a-subsection-of-a-matrix/18000/7 "2018-11-26T08:43:02Z")

</div>

Both the `view` and `copyto!` solutions suggested above allocate. Try this to avoid allocations:

```julia
copyto!(m, 1, m, 1 + size(m,1), (size(m,2) - 1) * size(m,1))

```

Or use a for loop.

---

<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, 8:45am UTC](https://discourse.julialang.org/t/what-is-the-quickest-way-to-move-a-subsection-of-a-matrix/18000/8 "2018-11-26T08:45:07Z")

</div>

I totally get that, and I fully understand the concern: how can you copy overlapping memory portions without making an extra copy first? I’m actually not sure how `copyto!` does it. However I’d point out that the documentation of `copyto!` does not specify that source and destination have to be different, right?

@bennedich: nice one! Pity it only works for full columns, unlike the `CartesianIndices` approach.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [November 26, 2018, 8:49am UTC](https://discourse.julialang.org/t/what-is-the-quickest-way-to-move-a-subsection-of-a-matrix/18000/9 "2018-11-26T08:49:08Z")

</div>

> [@pablosanjose](#):
>
> the documentation of `copyto!` does not specify that source and destination have to be different, right?

I think it would be worthwhile to open an issue about clarifying this. I get the impression that it is supposed to work, eg

> <https://github.com/JuliaLang/julia/issues/20069>
>
> The \`copy!\` function has special-casing to handle the case where the source and …destination arrays are equal, but this machinery does not work for views:
> \`\`\`jl
> julia\> a = \[1:10;\]; copy!(a, 2, a, 1, 9)
> 10-element Array{Int64,1}:
> 1
> 1
> 2
> 3
> 4
> 5
> 6
> 7
> 8
> 9
> 
> julia\> a = \[1:10;\]; v = view(a, 1:10); copy!(v, 2, v, 1, 9)
> 10-element SubArray{Int64,1,Array{Int64,1},Tuple{UnitRange{Int64}},true}:
> 1
> 1
> 1
> 1
> 1
> 1
> 1
> 1
> 1
> 1
> \`\`\`

Also, currently `copyto!` uses the `memmove` C function for this, which does handle overlap.

---

<div class="post-metadata">

**Author:** ![bennedich](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bennedich/32/4894_2.png) [@bennedich](https://discourse.julialang.org/u/bennedich)\
**Post date:** [November 26, 2018, 8:57am UTC](https://discourse.julialang.org/t/what-is-the-quickest-way-to-move-a-subsection-of-a-matrix/18000/10 "2018-11-26T08:57:02Z")

</div>

> [@pablosanjose](#):
>
> nice one! Pity it only works for full columns, unlike the `CartesianIndices` approach.

Yes, for other ranges, I’d just consider a for loop. Either loop over each individual element, or a loop of `copyto!`. It’s a bit frustrating how it’s sometimes so hard to avoid allocations for simple operations like this in Julia.

---

<div class="post-metadata">

**Author:** ![Raf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raf/32/3383_2.png) [@Raf](https://discourse.julialang.org/u/Raf)\
**Post date:** [November 26, 2018, 9:04am UTC](https://discourse.julialang.org/t/what-is-the-quickest-way-to-move-a-subsection-of-a-matrix/18000/11 "2018-11-26T09:04:38Z")

</div>

Thanks everyone. I should have specified strictly non-allocating methods! Copyto seems like the way to go, I was imagining something like memmove existed but couldn’t find it.

---

<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, 9:23am UTC](https://discourse.julialang.org/t/what-is-the-quickest-way-to-move-a-subsection-of-a-matrix/18000/12 "2018-11-26T09:23:15Z")

</div>

> [@bennedich](#):
>
> Yes, for other ranges, I’d just consider a for loop. Either loop over each individual element, or a loop of `copyto!` . It’s a bit frustrating how it’s sometimes so hard to avoid allocations for simple operations like this in Julia.

Is it clear why `copyto!` with `CartesianIndices` allocates, unlike a simple for? It’s using a loop itself

> <https://github.com/JuliaLang/julia/blob/eabc5de03131e780a8be3b58dd576f1007b9ce99/base/multidimensional.jl#L847>

---

<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:16am UTC](https://discourse.julialang.org/t/what-is-the-quickest-way-to-move-a-subsection-of-a-matrix/18000/13 "2018-11-26T10:16:22Z")

</div>

Hey again,

I’ve checked that the bit that allocates when using `copyto!` with `CartesianIndices` is precisely the `unalias` part, which in this case needs to make a copy of `m`, I guess. The interesting question is then how can `memmove` (which gets called directly when using @bennedich’s `copyto!` solution) avoid any allocation, and whether we could do something similar, and avoid calling `Base.unalias`. I suspect it’s not completely trivial, as the critical difference is that `memmove` can exploit the fact that the memory chunk to be moved is contiguous, so it’s easy to avoid a copy, but when using `CartesianIndices` it’s a little bit more involved, I guess.

---

<div class="post-metadata">

**Author:** ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)\
**Post date:** [November 26, 2018, 11:15am UTC](https://discourse.julialang.org/t/what-is-the-quickest-way-to-move-a-subsection-of-a-matrix/18000/14 "2018-11-26T11:15:09Z")

</div>

See also [Understanding performance using `@btime` and `@code_warntype`, `@code_llvm`, etc - #2 by tim.holy](https://discourse.julialang.org/t/understanding-performance-using-btime-and-code-warntype-code-llvm-etc/16293/2), which appears to apply here.

---

<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, 11:17am UTC](https://discourse.julialang.org/t/what-is-the-quickest-way-to-move-a-subsection-of-a-matrix/18000/15 "2018-11-26T11:17:51Z")

</div>

> [@tim.holy](#):
>
> See also [Understanding performance using `@btime` and `@code_warntype`, `@code_llvm`, etc](https://discourse.julialang.org/t/understanding-performance-using-btime-and-code-warntype-code-llvm-etc/16293/2), which appears to apply here.

You mean this?

```julia
julia> @btime copyto!($m, CartesianIndices((1:3, 1:2)), $m, CartesianIndices((1:3,2:3)));
  42.432 ns (1 allocation: 160 bytes)

julia> g(m) = copyto!(m, CartesianIndices((1:3, 1:2)), m, CartesianIndices((1:3, 2:3)))
g (generic function with 1 method)

julia> @btime g($m);
  42.460 ns (1 allocation: 160 bytes)

```

---

<div class="post-metadata">

**Author:** ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)\
**Post date:** [November 26, 2018, 11:20am UTC](https://discourse.julialang.org/t/what-is-the-quickest-way-to-move-a-subsection-of-a-matrix/18000/16 "2018-11-26T11:20:54Z")

</div>

No, I mean the size check inside `copyto!` itself uses string interpolation. Factor that out into a separate function and it might get faster.

---

<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, 11:25am UTC](https://discourse.julialang.org/t/what-is-the-quickest-way-to-move-a-subsection-of-a-matrix/18000/17 "2018-11-26T11:25:19Z")

</div>

Ah, right! No, for some reason this doesn’t seem to allocate anything extra (in v1.1 at least)

```julia
julia> function risky_copyto!(dest::AbstractArray{T1,2}, Rdest::CartesianIndices{2},
                         src::AbstractArray{T2,2}, Rsrc::CartesianIndices{2}) where {T1,T2}
           ΔI = first(Rdest) - first(Rsrc)
           src′ = Base.unalias(dest, src)
           for I in Rsrc
               @inbounds dest[I + ΔI] = src′[I]
           end
           dest
       end

julia> g(m) = risky_copyto!(m, CartesianIndices((1:3, 1:2)), m, CartesianIndices((1:3, 2:3)))
g (generic function with 1 method)

julia> @btime g($m);
  37.906 ns (1 allocation: 160 bytes)

```

---

<div class="post-metadata">

**Author:** ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)\
**Post date:** [November 26, 2018, 11:41am UTC](https://discourse.julialang.org/t/what-is-the-quickest-way-to-move-a-subsection-of-a-matrix/18000/18 "2018-11-26T11:41:44Z")

</div>

Right, the allocation came from the `unalias`, but I was wondering if the string interpolation might nevertheless be measurable.

It does seem that perhaps one could avoid the `unalias` call by using a branch that checks whether the first element of `Rdest` is within `Rsrc`; if so, do the copy in order of `Iterators.reverse(Rsrc)`.

---

<div class="post-metadata">

**Author:** ![yang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yang/32/5232_2.png) [@yang](https://discourse.julialang.org/u/yang)\
**Post date:** [November 26, 2018, 12:17pm UTC](https://discourse.julialang.org/t/what-is-the-quickest-way-to-move-a-subsection-of-a-matrix/18000/19 "2018-11-26T12:17:27Z")

</div>

you may be can do like this:

```julia
julia> A=[1 2 3;1 2 3;1 2 3];

julia> B1=circshift(A,(0,2));

julia> B=hcat(B1[:,1:2],B1[:,2])
3×3 Array{Int64,2}:
 2 3 3
 2 3 3
 2 3 3

```

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [November 26, 2018, 2:01pm UTC](https://discourse.julialang.org/t/what-is-the-quickest-way-to-move-a-subsection-of-a-matrix/18000/20 "2018-11-26T14:01:50Z")

</div>

For `unalias` allocating: [https://github.com/JuliaLang/julia/pull/26237](https://github.com/JuliaLang/julia/pull/26237).

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