function [out]=randomizeAO(m, n) % function [out]=randomize(in, n) % By Angel Otarola % aotarola@email.arizona.edu % % description: This routine generates a vector which includes 'n' unique % randomly generated indexes. % inputs are: % m = number of elements in the vector to be randomized. % n = number of elements to be included in the randomized output array % % output: % out=a vector which contains the indexes of the randomized vector % % Example: % ---If the original array has 660 elements and we want to generate a % sequence of 100 elements randomly picked from the total of 660 % possible indexes, we do the following: % % out=randomizeAO(660,100); % % % A more simple example is the following: % % out=randomizeAO(15,15); % % The ouput might give something like the following: % 5,9,1,12,15,3,7,11,10,13,6,14,4,8,2 % As noticed the order of the elements has been randomized and they % are all unique. x=[1:1:m]; %%% position indexes of the original vector of length m; %%% initilizes the state of the random number generators each time this %%% routine is called. This guarantees numbers out of a unique sequence of %%% every time. rand('twister',sum(100*clock)); %%% The following code produces 'n' random unique ocurrences indexes for i=1:n, %% produces 'n' random indexes not_done=1; %% operational flag if i==1, out(i)=floor(rand(1)*m)+1; %% this is the first index obtained in a random way else while not_done, %% this is to make sure the next random index is %% different than the ones already %% generated. rindex = floor(rand(1)*m)+1; %% produces a random index indexes=find(out == rindex); %% check if rindex is already in the ones generated if length(indexes) == 0, %% this is true if rindex is unique and has not been previously generated out(i) = rindex; %% stores the unique index generated in the output array not_done = 0; %% we are done end end end end out=out(:); %% makes sure the output array is a column vector