function [f,idx,fnot] = picksubset(m,num,seed) <m> is a matrix <num> is X indicating the size of the subset to pick out [X Y] where X is the total number of sets and Y is the set number to pull out. in this case, the sets are mutually exclusive and they collectively comprise the original <m> matrix. note that the number of things in each set may be slightly different. <seed> (optional) is the rand state to use. default: 0. return: <f> as a vector with a random subset of <m>. <idx> as a vector of the indices of the elements that we picked. <fnot> as a vector with the remaining elements of <m>. note that if you try to pick out a subset bigger than <m>, we will just return as many elements as there are in <m>. example: picksubset(randn(10,10),10)
0001 function [f,idx,fnot] = picksubset(m,num,seed) 0002 0003 % function [f,idx,fnot] = picksubset(m,num,seed) 0004 % 0005 % <m> is a matrix 0006 % <num> is 0007 % X indicating the size of the subset to pick out 0008 % [X Y] where X is the total number of sets and Y is the set number to pull out. 0009 % in this case, the sets are mutually exclusive and they collectively 0010 % comprise the original <m> matrix. note that the number of things in each 0011 % set may be slightly different. 0012 % <seed> (optional) is the rand state to use. 0013 % default: 0. 0014 % 0015 % return: 0016 % <f> as a vector with a random subset of <m>. 0017 % <idx> as a vector of the indices of the elements that we picked. 0018 % <fnot> as a vector with the remaining elements of <m>. 0019 % 0020 % note that if you try to pick out a subset bigger than <m>, 0021 % we will just return as many elements as there are in <m>. 0022 % 0023 % example: 0024 % picksubset(randn(10,10),10) 0025 0026 % input 0027 if ~exist('seed','var') || isempty(seed) 0028 seed = 0; 0029 end 0030 0031 % do it 0032 prev = rand('state'); 0033 rand('state',seed); 0034 len = length(m(:)); 0035 if isscalar(num) 0036 idx = subscript(randperm(len),1:min(num,len)); 0037 else 0038 numsets = num(1); 0039 whichset = num(2); 0040 indices = zeros(numsets,ceil(len/numsets)); 0041 indices(1:len) = randperm(len); 0042 idx = filterout(indices(whichset,:),0); 0043 end 0044 f = m(idx); 0045 fnot = m(setdiff(1:numel(m),idx)); 0046 rand('state',prev);