Permute works extremely slow with large arrays

6 views (last 30 days)
I have a rather large array 200*700*10000 points. In order to save my computational time I am using permute function. s=permute(s,[3 1 2]); However, permute is extremely slow and take the largest ammount of time for computation. Why it is so? What can I do, opr how can I replace this function to faster my computation? Thank You for Your answers. Best regards, Arseny

Answers (1)

Titus Edelhofer
Titus Edelhofer on 8 Jul 2015
Hi Arseny,
this is indeed a rather large array, it needs about 10.4 GB of memory. For the permutation, another 10.4 GB are used to copy the entire array. How much memory to you have? Does your machine have at least 24 GB? Even if it has that amount of memory, I'm not surprised that it takes some time ...
Why do you have to permute? Why can't you build up the array directly with the order of indices you need?
Titus
  2 Comments
Arseny Kubryakov
Arseny Kubryakov on 8 Jul 2015
Dear Titus! I need to do it for some following procedurs like interp1, nanmean and so on. However, Your comment seems to be useful. I will think now how to get rid of this regrid! Anyway, can You explain me one thing. I thought that Matlab did not use additional memory, when using reshape, regrid and so on. So, it is wrong, yes? Thank You.
Titus Edelhofer
Titus Edelhofer on 8 Jul 2015
Yes and no: a matrix is one large contiguous piece of memory. reshape does not need to copy, because the order of the entries does not change
>> A = [1 4; 2 5; 3 6]
A =
1 4
2 5
3 6
>> A = reshape(A, 2, 3)
A =
1 3 5
2 4 6
Note, in either case the values in memory are [1 2 3 4 5 6].
With permute this is different:
>> A = [1 4; 2 5; 3 6]
A =
1 4
2 5
3 6
>> permute(A, [2 1])
ans =
1 2 3
4 5 6
After the permute, the order is [1 4 2 5 3 6]. For this reordering, the memory needs to be copied ...
Titus

Sign in to comment.

Categories

Find more on Resizing and Reshaping Matrices in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!