Recycling

In the example code on Animations With Plot there is a bit of code in which a vector that already contains some data is then accessed using another vector. The second one has the indexes of the first one cycled round. The result is that a new vector is output, containing the contents of the first vector, cycled in the order indicated by the second one.

Here are some examples.


            # Start with the first 6 letters of the alphabet@
            x = ['a', 'b', 'c', 'd', 'e', 'f']
            n = length(x)
            inds = circshift(1:n, -2)
            r = x[inds]
            #=
            The result is that the vector r is a 6-element Vector{Char} containing the original vector cycled two step backwards:
            'c'
            'd'
            'e'
            'f'
            'a'
            'b'
            =#

            # A positive number in 'circshift' moves the contents forwards:
            x = ['a', 'b', 'c', 'd', 'e', 'f']
            n = length(x)
            inds = circshift(1:n, 2)
            r = x[inds]
            r = x[inds]
            #=
            The result is that the vector r is a 6-element Vector{Char} containing the original vector cycled two step forwards:
             'e'
             'f'
             'a'
             'b'
             'c'
             'd'
            =#
            

Note that this operation produces a new vector, the orginal one is left unchanged.

This idea gneralises to any selection of elements. In the following example, a vector called cards contains the ace, two and three of spades and ditto of clubs. They are in suit order in the cards vector. A vector called indxs is then used to create a new vector in which the aces, twos and threes are in that order.


                julia> cards = ["1S", "2S", "3S", "1C", "2C", "3C"]
                6-element Vector{String}:
                 "1S"
                 "2S"
                 "3S"
                 "1C"
                 "2C"
                 "3C"
                
                julia> indxs = [1,4,2,5,3,6]
                6-element Vector{Int64}:
                 1
                 4
                 2
                 5
                 3
                 6
                
                julia> cards[indxs]
                6-element Vector{String}:
                 "1S"
                 "1C"
                 "2S"
                 "2C"
                 "3S"
                 "3C"
            

A further generalisation of this idea is that the indexes used to select elements of the cards vector selects only a subset of them:


                julia> spades = [1,2,3]
                3-element Vector{Int64}:
                 1
                 2
                 3
                
                julia> cards[spades]
                3-element Vector{String}:
                 "1S"
                 "2S"
                 "3S"
                
                julia> clubs = [4,5,6]
                3-element Vector{Int64}:
                 4
                 5
                 6
                
                julia> cards[clubs]
                3-element Vector{String}:
                 "1C"
                 "2C"
                 "3C"