MATLAB: Fast way to replicate array

matrixrepmat

I have a matrix W of size ExF, and I have a matrix D of size NxFxNxE where D(n,:,n,:) = W'
Is there a fast way to replicate W to form D without having to loop through N
Thanks!

Best Answer

w=[1 2; ...
3 4; ...
5 6];
N=5;
E=size(w,1);
F=size(w,2);
D=repmat(reshape(w',1,F,1,E),N,1,N,1);