MATLAB: Reshaping / regrouping cell arrays

arraycellgroupingMATLABresize

I have 3 cell arrays. I would like to reshape these arrays so that each column is grouped into cells of a defined size. The amount of columns per array varies.
E.g. Start with matrix below;
[1] [2] [3]
[4] [5] [6]
[7] [8] [9]
[10] [11] [12]
I'm trying to group each column by the same value, say 2. Then each cell would have the dimensions 1X2. Then it would be formatted like;
[1;4] [2;5] [3;6]
[7;10] [8;11] [9;12]
And if the value was a number that didn't divide by the total rows, like 3, then it would be formatted with the remainder untouched;
[1;4;7] [2;5;8] [3;6;9]
[10] [11] [12]
How would I do that? I've tried reading the helpfiles and couldn't find any examples that looked similar.

Best Answer

Edit
A=num2cell(reshape(1:12,3,4)') %-----Example---
[n,m]=size(A)
k=3
p=fix(n/k)
AA=A(1:p*k,:)
B=mat2cell(cell2mat(AA),k*ones(1,p),ones(1,m))
nn=n-p*k
B=[B;A(k*p+1:k*p+nn,:)]
Related Question