MATLAB: Divide matrix to be sub-matrix

.

A is matrix:
A=[111;222;333;444;555;666;777;888;999;1000;2000]; %total number of A is 11
I wanna divide matrix A to be "n" matrix with the equal number of each matrix :
Example (with n=4), the result is:
A1=[111;222;333]; % contain 3 element


A2=[444;555;666]; % contain 3 element
A2=[777;888;999]; % contain 3 element
A2=[1000;2000]; % contain 2 element
In this example: 11/4=2.75, so choose the number of each matrix is 3. And the number of element in the last matrix will be the remaining
And, then, store the resulted matrix in cell:
A_cell={A1, A2, A3, A4}
How to get:
A_cell={[111;222;333] ,[444;555;666], [777;888;999], [1000;2000] }

Best Answer

Method one: mat2cell:
mat2cell(A,[3,3,3,2])
You can generate the second input like this:
>> G = 4; % pick number of groups
>> A = [111;222;333;444;555;666;777;888;999;1000;2000];
>> L = numel(A);
>> N = ceil(L/G); % max elements per group
>> V = [N*ones(1,G-1),1+mod(L-1,N)];
>> C = mat2cell(A,V);
>> C{:}
ans =
111
222
333
ans =
444
555
666
ans =
777
888
999
ans =
1000
2000
Method two: accumarray:
>> V = ceil((1:L)/N);
>> C = accumarray(V(:),A,[],@(v){v});
>> C{:}
ans =
111
222
333
ans =
444
555
666
ans =
777
888
999
ans =
1000
2000