-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfact.m
More file actions
65 lines (62 loc) · 2.17 KB
/
Copy pathfact.m
File metadata and controls
65 lines (62 loc) · 2.17 KB
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
57
58
59
60
61
62
63
64
65
classdef fact < handle
properties
trtNames
trtNums
numTrts
allSeqs
numAllSeqs
reductionNum=0
maxReduceTimes = 10000000
n
reducedSeq
balancedSeqNames
numUniqueSeq
end
methods
function obj = fact(trtNames,n)
obj.n = n;
obj.trtNames = trtNames;
obj.numTrts = length(trtNames);
obj.trtNums = 1:obj.numTrts;
obj.allSeqs = perms(obj.trtNums);
obj.numUniqueSeq = size(obj.allSeqs,1);
numReps = ceil(obj.n/size(obj.allSeqs,1));
%repeat the perms if we dont have less unique seqs than n
if numReps
obj.allSeqs = repmat(obj.allSeqs,numReps,1);
end
obj.numAllSeqs = size(obj.allSeqs,1);
obj.reduceFactor();
end
function reduceFactor(obj)
%this will both reduce to required length, and perform rand
%shuffle on seq matrix
obj.reductionNum = obj.reductionNum+1;
%this will be auctomatically balanced if n < numAllSeq
randsort = randperm(obj.numAllSeqs,obj.n);
%take a random selection of rows
obj.reducedSeq=obj.allSeqs(randsort,:);
end
function counterbalanceWith(obj,A)
numReductions=1;
while 1
obj.reduceFactor(); %this will give a new reduced version, and we loop until we balance :)
partiallyBallenced = areFactorsBallenced(A.reducedSeq,obj.reducedSeq,A.trtNums,obj.trtNums);
if partiallyBallenced
break
end
numReductions=numReductions+1;
if numReductions > obj.maxReduceTimes
error('Tried to reduce %i times. #Fail.',obj.maxReduceTimes);
end
end
disp('balanced')
end
function output = balancedOutput(obj)
output = cell(size(obj.reducedSeq));
for i=1:obj.numTrts
output(obj.reducedSeq==i)=obj.trtNames(i);
end
end
end
end