Matlab:将数据块随机分成相等大小的集合

我有一个大型数据集,我需要随机分成5个几乎相等大小的集合进行交叉验证。我很高兴以前使用
_crossvalind_
分成几组,但这次我需要一次将数据块分成这些组。 假设我的数据如下所示:
data = [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18];
然后我想将它们随机分成2组,例如2组。像这样
g1 = [3 4], [11 12]  
g2 = [9 10]  
g3 = [1 2], [15 16]  
g4 = [7 8], [17 18]  
g5 = [5 6], [13 14]
我想我可以通过一些for循环来做到这一点,但我猜想在matlab中必须有一个更具成本效益的方法:-) 有什么建议?     
已邀请:
我正在将您的需求解释为集合的随机排序,但在每个集合中,元素的排序与父集合保持不变。您可以使用
randperm
随机排序集的数量,并对元素使用线性索引。
dataElements=numel(data);%# get number of elements
totalGroups=5;
groupSize=dataElements/totalGroups;%# I'm assuming here that it's neatly divisible as in your example
randOrder=randperm(totalGroups);%# randomly order of numbers from 1 till totalGroups
g=reshape(data,groupSize,totalGroups)';             %'# SO formatting
g=g(randOrder,:);
不同的
g
行为您提供不同的分组。     
你可以将阵列(randperm)洗牌,然后将其分成相应的部分。
data = [10 20 30 40 50 60 70 80 90 100 110 120 130 140 150];
permuted = data(randperm(length(data)));
% padding may be required if the length of data is not divisible by the size of chunks
k = 5;
g = reshape(permuted, k, length(data)/k);
    

要回复问题请先登录注册