counter=0; numedges=8; numexp=4; numcols=min(2*numexp+1,numedges)+2 fp=zeros(1,numcols) termpair=[5 7 10 13]; termedges=[5 8]; v1=[0 1;0 2]; v2=[4 3]; v3=[1 4;1 5;7 4;7 5];% Aha when you took the length of v3 you wanted to get 1 but you got 2 ! So now we take the size(v3,1) e.g. we count the rows. v4=[2 6;2 7;2 8;3 6;3 7;3 8]; superv=[v1;v2;v3;v4]; for i=1:size(v1,1) for j=(1+size(v1,1)):(size(v1,1)+size(v2,1)) for k=(1+size(v1,1)+size(v2,1)):(size(v1,1)+size(v2,1)+size(v3,1)) for m=(1+size(v1,1)+size(v2,1)+size(v3,1)):(size(v1,1)+size(v2,1)+size(v3,1)+size(v4,1)) g=perms([j k m]); for b=1:size(g,1) clist=[]; for tn=1:length(termpair) if find(g(b,:)==termpair(tn))>0 clist=[clist,find(g(b,:)==termpair(tn))]; % find the first time a termpair comes up in the row end end c=min(clist); if c==size(g,2) % if it comes up at the end of the row ord=[i g(b,:)]; superlistv=superv(ord,:)'; superlistv(:); sl=superlistv(:)'; if length(sl)-length(unique(sl))==sum(sl==[sl(2:(numexp*2)) sl(1)]); counter=counter+1; fp(counter,:)=zeros(1,numcols); e=ones(1,length(sl)); usl=sl(logical(e-(sl==[sl(2:(numexp*2)) sl(1)]))); fp(counter,1:length(usl))=usl; end elseif isempty(c) g ord=[i g(b,:)]; superv superlistv=superv(ord,:)' superlistv(:); sl=[superlistv(:)' termedges(2)]; if length(sl)-length(unique(sl))==sum(sl==[sl(2:(numexp*2)+1) sl(1)]); counter=counter+1; size(fp) fp(counter,:)=zeros(1,numcols); e=ones(1,length(sl)); usl=sl(logical(e-(sl==[sl(2:(numexp*2)+1) sl(1)]))); fp(counter,1:length(usl))=usl; end ord=[i g(b,:)]; superlistv=superv(ord,:)'; superlistv(:); sl=[superlistv(:)' termedges(1)] ; if length(sl)-length(unique(sl))==sum(sl==[sl(2:(numexp*2)+1) sl(1)]); counter=counter+1; fp(counter,:)=zeros(1,numcols); e=ones(1,length(sl)); usl=sl(logical(e-(sl==[sl(2:(numexp*2)+1) sl(1)]))); fp(counter,1:length(usl))=usl; end end end end end end end counter ufp= unique(fp, 'rows') T6=length(find(ufp(:,7)==0))*80 T7=(length(find(ufp(:,8)==0))-length(find(ufp(:,7)==0)))*16 T8=(length(find(ufp(:,9)==0))-length(find(ufp(:,8)==0))+length(find(ufp(:,7)==0)))*2 falsepositves=T6+T7+T8