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?
The easiest way to initialize them are probably list comprehensions, for example: julia> [[Vector{Float64}(undef, 4) for _ = 1:5] for _ = 1:6] 5-element Array{Array{Array{Float64,1},1},1}: ...
Create an array filled with the value x . For example, fill(1.0, (10,10)) returns a 10x10 array of floats, with each element initialized to 1.0 . If x is an object reference, all elements will refer to the same object. fill(Foo(), dims) will return an array filled with the result of evaluating Foo() once.
Julia provides a very simple notation to create matrices. A matrix can be created using the following notation: A = [1 2 3; 4 5 6]. Spaces separate entries in a row and semicolons separate rows. We can also get the size of a matrix using size(A).
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)
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