Code covered by the BSD License  

Highlights from
latins

from latins by Jon Farrimond
Produces a latin square of required dimensions

latins(row,col);
function [rowout,colout] = latins(row,col);

% latins(row,col)
%
% Produces a latin square of the size specified in terms of
% colums and rows as requesed by the user outputting a CSV file that can
% be opened in wordpad or imported into excel, openoffice or any other
% spreadsheet program. This software uses the Mersenne Twister algorithm to
% produce its random values.
%
% J. A. Farrimond, 2008
% School of Pharmacy,
% University of Reading.

rand('twister',sum(100*clock)); % sets up rand function for use in the randperm function

maxvalue=row*col;
randomlist = randperm(maxvalue); % creates a vector of a random organisation of the required number of values
latinvalue = zeros(row,col);     % creates output matrix

rowflag = 1; % sets soem useful flags for itteration
colflag = 1;
vectorloc = 1;

%% Loop for the case of greater number of colums than rows
if (col > row)
    for rowflag = 1:row;
            for colflag = 1:col; 
                latinvalue(rowflag,colflag) = randomlist(vectorloc);
                colflag + 1;
                vectorloc = vectorloc + 1;
            end
        rowflag + 1;
    end
    
%% Loop for the case of greater number of rows than columns
          elseif (row > col)
                for rowflag = 1:row;
                        for colflag = 1:col; 
                            latinvalue(rowflag,colflag) = randomlist(vectorloc);
                            colflag + 1;
                            vectorloc = vectorloc + 1;
                        end
                    rowflag + 1;
                end
%% Loop for the case of an equal number of colums and rows          
                    else
                        for rowflag = 1:row;
                            for colflag = 1:col; 
                                latinvalue(rowflag,colflag) = randomlist(vectorloc);
                                colflag + 1;
                                vectorloc = vectorloc + 1;
                            end
                            rowflag + 1;
                        end
end

%% Data output
latinvalue % Output to screen
  filename = datestr(now,30); % Finds the data and time in the ISO8601 standard
  eval( [ 'csvwrite(''latinCSV', num2str(filename), '.dat'', latinvalue);' ] ) % Outputs a file with the ISO8601 timestamp to the base directory
  


Contact us at files@mathworks.com