Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Functioning of permutedims in Julia unclear

Tags:

julia

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?

like image 463
Akshat Mehrotra Avatar asked Jan 19 '20 16:01

Akshat Mehrotra


People also ask

How do you initialize a vector in Julia?

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}: ...

How do you fill an array in Julia?

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.

How do you create a matrix in Julia?

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).


1 Answers

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)
like image 114
Akshat Mehrotra Avatar answered Sep 20 '22 03:09

Akshat Mehrotra