MATLAB: Splitting up a matrix – how can I do it more efficient

for loopmatrix

Hi
Say I have a 200×8 matrix A. I want to split it up into (for instance) 10 parts, as follows:
[Rows 1-20 and all 8 columns, Rows 21-40 and all 8 columns, …, Rows 181-200 and all 8 columns].
Let's write this as:
[20×8 submatrix 1, 20×8 sumatrix 2, … , 20×8 submatrix 10]
in a next step, I want to concatenate all rows of each submatrix such that I get:
[160×1 submatrix 1, 160×1 submatrix2, …, 160 submatrix 10]
The numbers are just examples of course. So currently, I do it like this:
nrSplits = 10; %split up original matrix into 10
nrColumns = 8;
A = randn(200,8);
y = 200:nrSplits;
for i = 1:nrSplits
foo = A(1+y*(i-1):y*i,1:nrColumns);
foo = foo(:);
ASplit(i,:) = foo;
end
..which works, but it seems to me a bit inefficient and clumsy. Is there a better way to do this? Maybe there are functions which would be helpful but I don't know of..?
Many thanks

Best Answer

This will do your work efficiently.
%matrix size
n = 200;
m = 8;
%number of rows to cut
t = 20;
%generate matrix
A = randi(10,n,m);
%operate matrix manipulation programmatically
S = 1:t:n;
E = arrayfun( @(k) reshape(A(k:k+t-1,:),t*m,1) , S , 'UniformOutput', false );
E = cell2mat(E);
You can also change the parameters n,m,t.