-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlatinSquare.m
executable file
·56 lines (46 loc) · 1.01 KB
/
latinSquare.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
function out = latinSquare(numCond)
%This function returns a latin square (n x n array).
%This function is called with the syntax latinSquare(n).
global out;
out = zeros(numCond);
firstCol = 0;
firstRow = 0;
for row = 1:numCond
offset = row-1;
for col = 1:numCond
if row == 1
if col == 1
element = 1;
else
element = shiftUp(out(row,col-1),shiftOffset(col,numCond),numCond);
end
else
element = upOne(row-1,col,numCond);
end
out(row, col) = element;
end
end
function shift = shiftOffset(col,numCond)
n = col-1;
if mod(col,2) == 1
shift = n;
else
shift = numCond - n;
end
function upOut = upOne(r,c,numCond)
global out;
val = out(r,c);
if val >= numCond
upOut = 1;
else
upOut = val + 1;
end
function shiftedUp = shiftUp(start, shiftMag, numCond)
for i = 1:shiftMag
if start + 1 > numCond
start = 1;
else
start = start + 1;
end
end
shiftedUp = start;