If I have an array with the size of 33x180x360, how do I change it to the size of 33x360x180?
Thank you.
If I have an array with the size of 33x180x360, how do I change it to the size of 33x360x180?
Thank you.
If the order of elements remains unchanged, you may use reshape
:
julia> A = [i+j+k for i in 1:4, j in 1:3, k in 1:2]
4×3×2 Array{Int64, 3}:
[:, :, 1] =
3 4 5
4 5 6
5 6 7
6 7 8
[:, :, 2] =
4 5 6
5 6 7
6 7 8
7 8 9
julia> reshape(A, 3, 4, 2)
3×4×2 Array{Int64, 3}:
[:, :, 1] =
3 6 6 6
4 4 7 7
5 5 5 8
[:, :, 2] =
4 7 7 7
5 5 8 8
6 6 6 9
On the other hand, if you’re trying to permute the last two axes, you may use permutedims
:
julia> permutedims(A, (1, 3, 2))
4×2×3 Array{Int64, 3}:
[:, :, 1] =
3 4
4 5
5 6
6 7
[:, :, 2] =
4 5
5 6
6 7
7 8
[:, :, 3] =
5 6
6 7
7 8
8 9
I think permutedims is what I need. Many thanks!
Another suggestion would be to have a good look at Images
package eco-system. These packages would be tackling a lot of the stuff you seem to be on and have done some nice work on it.
For example, for the question in the post look at:
julia> using Images
help?> PermutedDimsArray
search: PermutedDimsArray permuteddimsview
PermutedDimsArray(A, perm) -> B
Given an AbstractArray A, create a view B such that the dimensions appear to be permuted. Similar
to permutedims, except that no copying occurs (B shares storage with A).
See also permutedims, invperm.
Examples
≡≡≡≡≡≡≡≡≡≡
julia> A = rand(3,5,4);
julia> B = PermutedDimsArray(A, (3,1,2));
julia> size(B)
(4, 3, 5)
julia> B[3,1,2] == A[1,2,3]
true