% Generate a Latin Square % ============================= % for Matlab R13 % version 1.0 (feb 2010) % % Inputs % ------------------ % N - Integer value for the size of the square. % % Inputs % ------------------ % M - Your Latin Square % % Other functions % ------------------- % RANDPERM(n) is a random permutation of the integers from 1 to n. % For example, RANDPERM(6) might be [2 4 5 6 1 3]. % % CUMSUM(X) is a vector containing the cumulative sum of the elements of X. % Example: If X = [0 1 2 % 3 4 5] % then cumsum(X,1) is [0 1 2 and cumsum(X,2) is [0 1 3 % 3 5 7] 3 7 12] % (c) Richard Craig % email: richard@richard-craig.co.uk function [M] = latsq(N) M = [randperm(N); ones(N-1,N)] ; % Generate Square M = rem(cumsum(M)-1,N) + 1 ; % Add one due to base zero