MATLAB: How to construct a 2D matrix Sbig of size (M*(2N+1),M*(2N+1)) from a 4D matrix S of size:S(2N+1,2N+1,M,M) where N and M are interger number

reshape 4d martix into 2d large matrix

Hi,
I have a 4D matrix S of size:S(2N+1,2N+1,M,M) where N and M are intergers. I was able to construct S(:,:,J,m) (using for loop for J=1:M and m=1:M) which are 2D (2N+1,2N+1) sub-matrices. I need to build a two dimensional big matrix Sbig of size: Sbig(M*(2N+1),M*(2N+1)) and find its transpose. I was only able to extract 2D matrices using squeeze(S(:,:,J,m)) but was not able to put them into big matrix Sbig(M*(2N+1),M*(2N+1)). I also used reshape another matrix "Ainv" of size (2N+1,M) into vector "C" using "reshape": C = reshape(T*Ainv, [M*(2*N+1) 1]), but it did not work for matrix since it orders all elements in column order.
I saw the pattern in Sbig matrix and wrote it in for loop:
for J=1:M
for m=1:M
Sbig( ((J-1)*(2*N+1):J*(2*N+1)), ((m-1)*(2*N+1):m*(2*N+1)) ) = squeeze(S(:,:,J,m));
end
end
but again getting errors:
?????Subscript indices must either
be real positive integers or
logicals.
Is there way to write elements of Sbig(M*(2N+1),M*(2N+1)) matrix using a (2N+1,2N+1) submatrices squeeze(S(:,:,J,m))?

Best Answer

You start m as 1, so m-1 starts as 0, so (m-1)*(2*N+1) is going to be 0. That's why you get the error message. You need to add 1 to the subscript.