Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to switch axes (dimensions) in Julia for n-dimensional array

I have an array that I would like to switch the axes order of. It is similar to a transpose except I would like to perform it on arrays with dimensions greater than 2. In Python I would use np.transpose and in Matlab, permute, but I can't seem to find this in Julia. For instance,

a = ones(2, 3, 4)
size(a)
(2,3,4)

From this I would like to get an array of shape (3, 4, 2) by rearranging the axes (dimensions) to (2, 3, 1). I am looking for a function called new_func.

b = new_func(a, (2, 3, 1))
size(b)
(3,4,2)
like image 629
jtorca Avatar asked Jul 20 '14 00:07

jtorca


1 Answers

According to Stefan Karpinski, the answer is Base.permutedims (docs).

Example:

a = ones(2, 3, 4)
size(a) # => (2,3,4)

b = permutedims(a, [2, 3, 1])
size(b) # => (3,4,2)
like image 97
Kipton Barros Avatar answered Oct 13 '22 01:10

Kipton Barros