MATLAB: How to assign parts of a main matrix equal to the indexed sub matrices

matrix manipulationsliding window

In matlab if we have a main matrix
A=[1 2 1 2;3 4 3 4;1 2 1 2;3 4 3 4]
which is a 4*4 matrix divided into 2*2 sub blocks. If we want to get all the i'th values (1st,2nd,3rd,4th) of each sub matrix indexed to a single matrix.i.e., what I have to do to get the output as :
AA(1)=[1 1;1 1] ;
AA(2)=[2 2;2 2] ;
AA(3)=[3 3;3 3];
AA(4)=[4 4;4 4]
what if the size of main matrix is large say 512*348?

Best Answer

This will work for any size matrix:
cat(3,...
A(1:2:end,1:2:end),...
A(1:2:end,2:2:end),...
A(2:2:end,1:2:end),...
A(2:2:end,2:2:end))
If you want an arbitrary number of i-th submatrices, then use reshape and permute.