'Functioning of permutedims in Julia unclear
What does permutedims()
do when called upon a multidimensional array?
From its name, it is evident it has something to do with the dimentions of the array. However, when running the code below, the output is unexpected and not clear.
A = Array{Int64}(undef, 100,100,100)
B = permutedims(A, (1,2,3))
println(A == B)
Output:
`true`
So does it create a copy of the original array? and what is the use of the tuple passed?
Solution 1:[1]
The docs of Julia sometimes do not have a complete explanation on a given topic.
permutedims(A::AbstractArray, perm)
perms
is a tuple specifying the new order for the dimensions of A, where 1 corresponds to the first dimension (rows), 2 corresponds to the second dimension (columns), 3 corresponds to pages, and so on i.e. this function will return a copy of the array with its dimensions according to the specified perms
.
What happened in the code in the question is that by passing the tuple (1,2,3)
, we were telling Julia that place the first dim of A in the place of the first dim of B and the second in the place of second and so on. This basically created a copy of the array A
.
USE CASE EXAMPLE
A = ones(10,20,30) # Creates an array full of 1 of the size (10,20,30)
B = permutedims(A, (3,1,2))
println(A == B)
println(size(B))
OUTPUT
false
(30, 10, 20)
Solution 2:[2]
There are a few ways of using permutedims
. It is useful as a non-recursive version of LinearAlgebra.transpose
. When you want to convert a vector to a row vector or vice-versa, and the vector elements are simple (not other vectors or arrays), do this:
julia> v = [1, 2]
2-element Array{Int64,1}:
1
2
julia> v_row = permutedims(v)
1×2 Array{Int64,2}:
1 2
To transpose a matrix where the elements are simple, do this:
julia> m = [ 1 2
3 4 ]
2×2 Array{Int64,2}:
1 2
3 4
julia> permutedims(m, (2,1))
2×2 Array{Int64,2}:
1 3
2 4
julia> m = [ 1 2 3
4 5 6 ]
2×3 Array{Int64,2}:
1 2 3
4 5 6
julia> permutedims(m, (2,1))
3×2 Array{Int64,2}:
1 4
2 5
3 6
To permute the values of an n-dimensional array by each dimension, use permutedims(array, (newfirst_dim, newsecond_dim, ..))
, where newfirst_dim
, newsecond_dim
... are each one of the available dimensions 1:n and all dimensions are used:
julia> a = reshape(Vector(1:(2*3*4)), (2,3,4))
2×3×4 Array{Int64,3}:
[:, :, 1] =
1 3 5
2 4 6
[:, :, 2] =
7 9 11
8 10 12
[:, :, 3] =
13 15 17
14 16 18
[:, :, 4] =
19 21 23
20 22 24
julia> permutedims(a, (1,2,3)) # identity
2×3×4 Array{Int64,3}:
[:, :, 1] =
1 3 5
2 4 6
[:, :, 2] =
7 9 11
8 10 12
[:, :, 3] =
13 15 17
14 16 18
[:, :, 4] =
19 21 23
20 22 24
julia> permutedims(a, (3,2,1)) # reverse the dims
4×3×2 Array{Int64,3}:
[:, :, 1] =
1 3 5
7 9 11
13 15 17
19 21 23
[:, :, 2] =
2 4 6
8 10 12
14 16 18
20 22 24
julia> # the new last dimension is the old first dimension (length=2)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Nathan Musoke |
Solution 2 | Jeffrey Sarnoff |