MATLAB: How to split one dimensional array into 3 equal chunks

array splitdatadoublematrix array

Hi, I have the attached data array as an example, I need to split it into 3 equal chunks! Then I need to apply that for any other array with the same type but with different length.

Best Answer

Try this:
s = load('Cb.mat');
Cb = s.Cb;
var3c = @(oldvar) mat2cell(oldvar(:), [fix(numel(oldvar)/3) *[1, 1], numel(oldvar)-2*fix(numel(oldvar)/3)], 1); % Create New Matrix From Original Vector
newCb = var3c(Cb)
newCb =
3×1 cell array
{1632×1 double}
{1632×1 double}
{1633×1 double}
You cannot divide your vector equally, although you can get close. The ‘var3c’ anonymous function works with any length vector. It returns a cell array of 3 column vectors, even if the original vector is a row vector.