I am trying to rotate a 3D array in julia as if it represents a physical object in 3D space. Essentially, I want to know if there is a way to rotate an array by increments of 90 degrees along the x-, y-, and/or the z-axis.
In 2D it would be something like this if I were to rotate counter-clockwise...
1 2 3 3 6 9
4 5 6 -----> 2 5 8
7 8 9 1 4 7
I would want the same logic to apply in 3D as well.
Any help is appreciated.
For two-dimensional Matrices you have functions such as rotl90
, rotr90
and rot180
. Those can be combined with mapslices
to get the desired effect. For an example below is a rotation over dimensions 1
and 2
for each cut of array in the dimension 3
.
julia> A=collect(reshape(1:27,3,3,3))
3×3×3 Array{Int64,3}:
[:, :, 1] =
1 4 7
2 5 8
3 6 9
[:, :, 2] =
10 13 16
11 14 17
12 15 18
[:, :, 3] =
19 22 25
20 23 26
21 24 27
julia> mapslices(rotr90,A,dims=[1,2])
3×3×3 Array{Int64,3}:
[:, :, 1] =
3 2 1
6 5 4
9 8 7
[:, :, 2] =
12 11 10
15 14 13
18 17 16
[:, :, 3] =
21 20 19
24 23 22
27 26 25
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With