Sei sulla pagina 1di 1

function randomizeDemo

% randomizeDemo Example of randomizing the order of a single-factor test


% Use example problem from data from D.C. Montgomery,
% _Design and Analysis of Experiments_, 5th ed., 2001,
% Wiley, New York. See Chapter 3, pp. 60-62
%
% No inputs to the main program

% -- Set up the trial data


nreps = 5; % number of repetitions per treatment
reps = ones(1,nreps); % Temporary vector to create list of treatments

% -- CWP is the Cotton weight percents, SID is a list of labels (IDs)


CWP = [ 15*reps, 20*reps, 25*reps, 30*reps, 35*reps];
SID = 1:length(CWP);

% -- Randomize the test order


[CWPrand,SIDrand] = randomize(CWP,SID);
fprintf('\n Test Sample Cotton Weight\nSequence ID Percent\n');
for i=1:length(CWPrand)
fprintf('%4d %6d %6d\n',i,SIDrand(i),CWPrand(i));
end

% ===============================================
function [xrand,idrand] = randomize(x,id)
% randomize Randomize a vector of test conditions x with corresponding IDs
%
% Synopsis: xrand = randomize(x)
% [xrand,idrand] = randomize(x)
% [xrand,idrand] = randomize(x,id)
%
% Input: X = vector of values to be put in random order
% id = optional vector if IDs for the x data
%
% Output: xrand = values of the x vector in random order
% idrand = optional vector of ID values for to the elements in x
% If no id vector is supplied, but idrand is expected as
% a return value, generate the IDs as sequential integers

% -- Generate a random vector, sort it, and save the sort order.
irand = randperm(length(x)); % randomized list of integers
[junk,isort] = sort(irand); % isort is the sort order for the integers
xrand = x(isort);

% -- Sort IDs if user either supplies IDs or asks for IDs


if nargin>1
idrand = id(isort); % If IDs were supplied, sort them too
elseif nargout>1 % No IDs were supplied, generate some & then sort
id = 1:length(x); % Generate sequential IDs
idrand = id(isort); % and sort them in the same order as xrand
end

Potrebbero piacerti anche