Permute works extremely slow with large arrays

9 次查看(过去 30 天)
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

回答(1 个)

Titus Edelhofer
Titus Edelhofer 2015-7-8
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 个评论
Arseny Kubryakov
Arseny Kubryakov 2015-7-8
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 2015-7-8
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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by