Stream: helpdesk (published)

Topic: Shift indices


view this post on Zulip Michael Alexander (Mar 09 2021 at 15:21):

Is there a function that easily shifts arrays of indices? For example, I have an array x =[-2, -1, 0, 1, 2] and want to make it into an array of indices y =[4, 5, 1, 2, 3].

I thought about Offset arrays, but I don't really need the whole array shifted and the shift of x changes depending on the input values.

view this post on Zulip Andrey Oskin (Mar 09 2021 at 15:55):

Something like?

mod1.(x .+ 6, 5)

view this post on Zulip Mason Protter (Mar 09 2021 at 16:43):

Have you see CircularArrays.jl? Not sure if this can totally address your usecase or not.

view this post on Zulip Mason Protter (Mar 09 2021 at 16:47):

julia> using CircularArrays

julia> a = CircularVector([:a, :b, :c, :d, :e]);

julia> getindex.((a,), [-2, -1, 0, 1, 2] .+ 1)
5-element Vector{Symbol}:
 :d
 :e
 :a
 :b
 :c

julia> getindex.((a,), [4, 5, 1, 2, 3])
5-element Vector{Symbol}:
 :d
 :e
 :a
 :b
 :c

view this post on Zulip Michael Alexander (Mar 09 2021 at 17:14):

mod1 is perfect! Exactly fits the bill

CircularArrays or OffsetArrays probably would work, but there is only one function that requires this shift and the rest are handled as regular arrays, so I was trying to avoid mixing array types.

view this post on Zulip Toby Driscoll (Mar 16 2021 at 18:01):

Are there packages for shifting/circular arrays that respect linear algebra? For example, if I have a finite difference value vector numbered 0:n, I can multiply it by a matrix that has column indices 0:n as well. Or if I get the eigenvectors of a circularly defined Fourier transform matrix, they are circular vectors.

view this post on Zulip Mason Protter (Mar 16 2021 at 18:07):

Wow, I never realized that OffsetArrays.jl didn't work with linear algebra. That's a little disappointing. It should at least deal with multiplication


Last updated: Oct 02 2023 at 04:34 UTC