有规则随机数建立代码

今天为了建立一个随机数有规则的矩阵,方法有点笨,不过还是想出来啦,哈哈哈。开心,为自己的努力喝彩……


   type1=randperm(100);
  trn1=type1(1:80);
  tst1=type1(81:100);
 
%  newtype1=type1';
%  oldtype1=sortrows(newtype1,size(newtype1,2));

  type2=101:200;
  type2= type2(randperm(numel(type2)));
  trn2=type2(1:80);
  tst2=type2(81:100);
 
  type3=201:300;
  type3= type3(randperm(numel(type3)));
  trn3=type3(1:80);
  tst3=type3(81:100);
 
   type4=301:400;
   type4= type4(randperm(numel(type4)));
   trn4=type4(1:80);
   tst4=type4(81:100);
 
  type5=401:500;
  type5= type5(randperm(numel(type5)));
  trn5=type5(1:80);
  tst5=type5(81:100);
 
  type6=501:600;
  type6= type6(randperm(numel(type6)));
  trn6=type6(1:80);
  tst6=type6(81:100);
 
  type7=601:700;
  type7= type7(randperm(numel(type7)));
  trn7=type7(1:80);
  tst7=type7(81:100);
  
  trn= [trn1 trn2 trn3 trn4 trn5 trn6 trn7];
  tst=[tst1 tst2 tst3 tst4 tst5 tst6 tst7];
 
在此献上大牛帮我写的简短代码,实现效果一样,不过为什么我的代码就那么长呢,很简单,因为我很笨。谢谢你,我的贵人。

 for i = 1:100:700
       a(1,i:i+99) = randperm(100)+(i-1);
 end
train = zeros(1,560);
j =1;
for i=1:100:700
     train(1,j:(j+80-1)) = a(1,i:((i-1)+80));
     j = j+80;
end

% test = zeros(1,140);
% k =1;
% for i=80:100:700
%      test(1,k:(k+20-1)) = a(1,(i+1):(i+20));
%      k = k+20;
% end

test1 = zeros(1,140);
k=1;
for i =1:100:700
    test1(1,k:(k+20-1)) = a(1,(i+80):(i+100-1));
     k = k+20;
end


你可能感兴趣的:(有规则随机数建立代码)