MATLAB: Split variable length vector by parts

splitsplit_by_partssubarrayssubvectorvariable_length_vector

So it's probably an easy solution, but can't really find the answer that quick.
I have the following vector:
V = [1 2 3 4 5 ... n];
P = ceil(length(V/127)); %dont know if ceil or floor is better
And then I want to split the vector `V` in parts `P`, so that I'll get something like the following:
% as an example n = 2184 & P = 17
C = {[1 2 3 ... 18],[18 19 20 ... 35],...[2167 2168 2169 ...2184]};
Thanks in advance

Best Answer

L-length(V);
P=17;
id2=[repmat(P,1,fix(L/P)) rem(L,P)]; % number elements in cell size P matching L plus remainder
C=mat2cell(V,1,id2);
That's for the nearest multiple of P with whatever is left over tacked onto the end irregardless of its size relative to P. If it's needed to make as near even as possible need more logic to choose the bin size; that's not a given criterion in the question posed, however.