Assuming that the number of input matrix rows/columns are exact multiples of the number of submatrix rows/columns, then this does the job quite nicely:
A = [1,2,1,2;3,4,3,4;1,2,1,2;3,4,3,4]
[Ar,Ac] = size(A);
Nr = 2;
Nc = 2;
Sr = Ar/Nr;
Sc = Ac/Nc;
C = cell(Sr,Sc);
for Kr = 1:Sr
for Kc = 1:Sc
C{Kr,Kc} = A(Kr:Sr:end,Kc:Sc:end);
end
end
You can easily concatenate the cell array contents into one numeric array:
There are probably ways of doing this using reshape and permute, but the loop is easier to understand and is likely reasonably efficient anyway. Tested on the matrix from the original question: >> A = [1,2,1,2;3,4,3,4;1,2,1,2;3,4,3,4]
A =
1 2 1 2
3 4 3 4
1 2 1 2
3 4 3 4
>> Nr = 2;
>> Nc = 2;
...
>> C{:}
ans =
1 1
1 1
ans =
3 3
3 3
ans =
2 2
2 2
ans =
4 4
4 4
And a larger example:
>> A = [1,2,3,4,1,2,3,4;5,6,7,8,5,6,7,8];
>> A = [A;A;A;A]
A =
1 2 3 4 1 2 3 4
5 6 7 8 5 6 7 8
1 2 3 4 1 2 3 4
5 6 7 8 5 6 7 8
1 2 3 4 1 2 3 4
5 6 7 8 5 6 7 8
1 2 3 4 1 2 3 4
5 6 7 8 5 6 7 8
>> Nr = 4;
>> Nc = 2;
...
>> C{:}
ans =
1 1
1 1
1 1
1 1
ans =
5 5
5 5
5 5
5 5
ans =
2 2
2 2
2 2
2 2
ans =
6 6
6 6
6 6
6 6
ans =
3 3
3 3
3 3
3 3
ans =
7 7
7 7
7 7
7 7
ans =
4 4
4 4
4 4
4 4
ans =
8 8
8 8
8 8
8 8
Best Answer