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

2d array3d arrayfor loop

Hello,
Lets say I have 2D array x(1100×5), 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?

Best Answer

xt=x.';
idx=(d-w+1:d-2).' + (1:n1-d);
a=permute( reshape( xt(:,idx(:)) ,5,[],n1-d) ,[2,1,3]) ;
Related Question