How to create 3D array out of 2D, with one row step without using for loop?

2 ビュー (過去 30 日間)
Mantas Vaitonis
Mantas Vaitonis 2018 年 7 月 3 日
コメント済み: Mantas Vaitonis 2018 年 7 月 4 日
Hello,
Lets say I have 2D array x(1100x5), n1=1100,d=501 and w=500. I can create 3D array, with one row step using for loop, however it is very slow working with bigger arrays:
for k=1:n1-d
a(:,:,k)=x(d-w+k-1:d+k-2,:);
end
What would be the way without using for loop?
  2 件のコメント
John D'Errico
John D'Errico 2018 年 7 月 3 日
It is slow mainly because you do not understand the importance of preallocating your array, thus a. When you grow an array as you did, you force MATLAB to reallocate that memory each time in the loop. This gets slow, because the memory needed increases in size each time through.
Mantas Vaitonis
Mantas Vaitonis 2018 年 7 月 3 日
Thank You for Your answer, regarding preallocation, I do this a=zeros(500,5,599,'gpuArray'); and accordingly when working with bigger arrays, but its little help.

サインインしてコメントする。

採用された回答

Matt J
Matt J 2018 年 7 月 3 日
xt=x.';
idx=(d-w+1:d-2).' + (1:n1-d);
a=permute( reshape( xt(:,idx(:)) ,5,[],n1-d) ,[2,1,3]) ;

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by