This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function chuckCell = splitvect(v, n) | |
% Splits a vector into number of n chunks of the same size (if possible). | |
% In not possible the chunks are almost of equal size. | |
% | |
% based on http://code.activestate.com/recipes/425044/ | |
chuckCell = {}; | |
vectLength = numel(v); | |
splitsize = 1/n*vectLength; | |
for i = 1:n | |
%newVector(end + 1) = | |
idxs = [floor(round((i-1)*splitsize)):floor(round((i)*splitsize))-1]+1; | |
chuckCell{end + 1} = v(idxs); | |
end | |
assert(sum(cellfun(@numel, chuckCell)) == vectLength, ... | |
'More or less elements after split') |
%split into 3 parts
chunckCell=splitvect(1:10, 3);
chunckCell{:}
ans =
1 2 3
ans =
4 5 6 7
ans =
8 9 10