Use Randperm () to generate random seeds and then swap positions to achieve the objective of randomly extracting samples.
Official content:
Matlab comes with a function randperm (n) to produce an integer of 1 to n without repeating random permutation, which can be used to obtain a random number without repetition.
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 is [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));
Principle:
1. Rand (1, N) produces a random number matrix within 0-1 of the 1 Rows n column.
2. Sort () sorts the matrix, the returned ignore is the sorted sequence, p is the original index of the number of sorted sequences, the index is definitely random, and there are no duplicate integers between 1 and N.
On the code:
>> 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.66 65 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)); The% immediately disrupts the array index A = A (Randindex,:); % constructed with new index scrambled array >> 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.69 81 0.1904 0.4820 0.4228 0.6665 0.3689 0.1206
Methods for randomly disrupting training samples in MATLAB