Rotating non-bit arrays in Julia

Using rol(a,1) on a=BitArray([true,true,false]) gives the following result:

 julia> rol(a,1) 3-element BitArray{1}: true false true 

How can I rotate non-bit arrays? For example, I had b=[1,2,3,4] and would like to get the following conclusion: [2,3,4,1] .

+7
arrays julia-lang
source share
2 answers

you are looking for circshift :

 julia> circshift([1,2,3,4], -1) 4-element Array{Int64,1}: 2 3 4 1 
+8
source share

circshift does what you want according to the accepted answer. However, to add to this, you can also manually manipulate indexes:

 julia> a = [1,2,3,4]; julia> a[[2:end; 1]] |> show [2,3,4,1] julia> a[[3:end; 1:2]] |> show [3,4,1,2] 

In fact, what you are doing here is indexing the integers 'linear' , so the number of elements can be more than 4 (i.e. the same positions may be available several times or not at all):

 julia> a[[3:end; 1:2; 1; 2; 2; 1]] |> show [3,4,1,2,1,2,2,1] julia> a[[1:end; end-1:-1:1]] |> show [1,2,3,4,3,2,1] 

The same applies to arrays with a higher dimension:

 julia> b = [1 2 3 4; 2 3 4 5; 3 4 5 6; 4 5 6 7]; julia> b[[2:end; 1], [1:3; 2:end; 1]] 4ร—7 Array{Int64,2}: 2 3 4 3 4 5 2 3 4 5 4 5 6 3 4 5 6 5 6 7 4 1 2 3 2 3 4 1 
+1
source share

All Articles