matlab中訓練樣本隨機打亂的方法,matlab樣本

來源:互聯網
上載者:User

matlab中訓練樣本隨機打亂的方法,matlab樣本

使用randperm(),產生隨機種子,然後交換位置,達到隨機抽取樣本的目的。

官方內容:

Matlab內建函數randperm(n)產生1到n的整數的無重複的隨機排列,利用它就可以得到無重複的隨機數。
function p = randperm(n);
%RANDPERM Random permutation.
% RANDPERM(n) is a random permutation of the integers from 1 to n.
% For example, RANDPERM(6) might be [2 4 5 6 1 3].
%
% Note that RANDPERM calls RAND and therefore changes RAND's state. %
% See also PERMUTE. % Copyright 1984-2002 The MathWorks, Inc.
% $Revision: 5.10 $ $Date: 2002/04/09 00:26:14 $
[ignore,p] = sort(rand(1,n));


原理:
1. rand(1, n)產生1行n列的0-1之內的隨機數矩陣。
2. sort()把這個矩陣排序,返回的ignore是排序後的序列,p是排序後的序列的各數原來的索引,這個索引肯定是隨機的,而且是在1到n間無重複的整數。


上代碼:

>> a = rand(10,4)a =    0.9516    0.3015    0.0326    0.6448    0.9203    0.7011    0.5612    0.3763    0.0527    0.6663    0.8819    0.1909    0.7379    0.5391    0.6692    0.4283    0.2691    0.6981    0.1904    0.4820    0.4228    0.6665    0.3689    0.1206    0.5479    0.1781    0.4607    0.5895    0.9427    0.1280    0.9816    0.2262    0.4177    0.9991    0.1564    0.3846    0.9831    0.1711    0.8555    0.5830>> RandIndex = randperm( length( a ) );   % 隨即打亂數組索引a = a( RandIndex,: );   % 用新的索引構造打亂後的數組>> aa =    0.9831    0.1711    0.8555    0.5830    0.9516    0.3015    0.0326    0.6448    0.7379    0.5391    0.6692    0.4283    0.9203    0.7011    0.5612    0.3763    0.9427    0.1280    0.9816    0.2262    0.4177    0.9991    0.1564    0.3846    0.0527    0.6663    0.8819    0.1909    0.5479    0.1781    0.4607    0.5895    0.2691    0.6981    0.1904    0.4820    0.4228    0.6665    0.3689    0.1206


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.