这是使用函数
linspace,
round和
diff的简单解决方案:
[M, N] = size(mat); % Matrix size
nSub = 10; % Number of submatrices
cMat = mat2cell(mat, diff(round(linspace(0, M, nSub+1))), N);
这种方法将在单元阵列的最终单元格中以更均匀的方式分配额外的行.请注意使用mat = magic(5)时应用上述输出; (左)和mat = magic(13); (对):
cMat = cMat =
[1x5 double] [1x13 double]
[0x5 double] [2x13 double]
[1x5 double] [1x13 double]
[0x5 double] [1x13 double]
[1x5 double] [2x13 double]
[0x5 double] [1x13 double]
[1x5 double] [1x13 double]
[0x5 double] [1x13 double]
[1x5 double] [2x13 double]
[0x5 double] [1x13 double]
如果你更喜欢随机分配额外的行,你可以像这样使用randperm:
subSizes = diff(round(linspace(0, M, nSub+1)));
cMat = mat2cell(mat, subSizes(randperm(nSub)), N);