How to create 3D array out of 2D, with one row step without using for loop?
조회 수: 1 (최근 30일)
이전 댓글 표시
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
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.
채택된 답변
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 Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!