# Move elements between vectors

**URL:** <https://discourse.julialang.org/t/move-elements-between-vectors/83825>\
**Category:** General Usage\
**Tags:** performance, arrays\
**Created:** [July 6, 2022, 7:38am UTC](https://discourse.julialang.org/t/move-elements-between-vectors/83825 "2022-07-06T07:38:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [July 6, 2022, 7:38am UTC](https://discourse.julialang.org/t/move-elements-between-vectors/83825/1 "2022-07-06T07:38:32Z")

</div>

what is an efficient way to move an element from one position in one vector to another position in another vector?  
And in particular from the first position of the transferring vector to the last position of the receiving vector?

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [July 6, 2022, 7:40am UTC](https://discourse.julialang.org/t/move-elements-between-vectors/83825/2 "2022-07-06T07:40:48Z")

</div>

`dest[end] = src[begin]`

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [July 6, 2022, 7:43am UTC](https://discourse.julialang.org/t/move-elements-between-vectors/83825/3 "2022-07-06T07:43:53Z")

</div>

I better specify the request.

If the two vectors are S (recipient) and cc (donor) the solution must be something equivalent to

```julia
      push!(S,cc[1])
      deleteat!(cc,1)

```

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [July 6, 2022, 7:51am UTC](https://discourse.julialang.org/t/move-elements-between-vectors/83825/4 "2022-07-06T07:51:01Z")

</div>

`push!(S, popfirst!(cc))`

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [July 6, 2022, 12:37pm UTC](https://discourse.julialang.org/t/move-elements-between-vectors/83825/5 "2022-07-06T12:37:48Z")

</div>

I was thinking of something even more low-level.  
I also used the popfirst function. But it’s no better than deleteat !.

```julia
julia> function isolapop(c, cidx, CI)
           S=CartesianIndex{2}[]
           ci=[c]
           while !isempty(ci)
               cc=copy(ci)
               for i in ci
                   if !∈(i, S)
                       push!(S,i)
                       popfirst!(cc)
                       #deleteat!(cc,1)
                   end
                   for n in CI
                       x=i+n
                       if (x ∈ cidx) && !∈(x,cc) && !∈(x,ci) && !∈(x,S)  

                           push!(cc,x)
                       end
                   end
               end
               ci=cc
           end
          S
       end
isolapop (generic function with 1 method)

julia> function isoladelat(c, cidx, CI)
           S=CartesianIndex{2}[]
           ci=[c]
           while !isempty(ci)
               cc=copy(ci)
               for i in ci
                   if !∈(i, S)
                       push!(S,i)
                       #popfirst!(cc)
                       deleteat!(cc,1)
                   end
                   for n in CI
                       x=i+n
                       if (x ∈ cidx) && !∈(x,cc) && !∈(x,ci) && !∈(x,S)  

                           push!(cc,x)
                       end
                   end
               end
               ci=cc
           end
          S
       end
isoladelat (generic function with 1 method)

julia> @btime isolapop(cidx[1],cidx, CI)
  758.929 ns (14 allocations: 1.84 KiB)
10-element Vector{CartesianIndex{2}}:
 CartesianIndex(1, 1)
...
 CartesianIndex(6, 2)

julia> @btime isoladelat(cidx[1],cidx, CI)
  762.617 ns (14 allocations: 1.84 KiB)
10-element Vector{CartesianIndex{2}}:
 CartesianIndex(1, 1)
 CartesianIndex(1, 2)
 ...
 CartesianIndex(6, 2)

```

The problem stems from [this discussion](https://app.slack.com/client/T68168MUP/threads/thread/C674VR0HH-1657103244.026059) … where I was trying to modify various aspects of the functions to make them more efficient.
