function [trans,field,lrec]=gentransnet(tokens,c,thresh) % c = insensitivity factor % thresh = cutoff for similarity %% Create 'field' start=10000*ones(size(tokens{1},1),1); % 'start' state stop=start*2; % 'stop' state field=[]; for i=1:length(tokens) field=[field,tokens{i}]; % iteratively concatenate lrec(i)=size(tokens{i},2); end field=[start,field,stop]; % circumfix start/stop states F=size(field,2); % v=var(field');v(v==0)=inf; % this has to do with weighting % the features, ignore for now % Create 'trans' % trans=spalloc(F,F,3e7); % it would be nice to keep it % sparse to reduce memory, but % Matlab is bad at accessing % chunks of sparse matrices r=min(size(tokens{1},1),13); %% Initializations trans=zeros(F,F); starts=[0,cumsum(lrec(1:end-1))]+2; stops=cumsum(lrec)+1; %% Main loop for i=1:length(tokens) for j=i:length(tokens) % A=rowkron(1./v',tokens{i}); % feature weighting, ignore % B=rowkron(1./v',tokens{j}); % A=tokens{i}(1:13,:);B=tokens{j}(1:13,:); A=tokens{i}(1:r,:);B=tokens{j}(1:r,:); [r,cA]=size(A);[cB]=size(B,2); A=A(:,:,ones(cB,1)); B=reshape(B,r,1,cB);B=B(:,ones(cA,1),:); s=exp(-c*sqrt(squeeze(sum((A-B).^2)))); % similarity s=(s>thresh).*s; % zero out if below threshold % patch the token matrices into a big quilt trans(starts(i):stops(i),starts(j)+1:stops(j)+1)=sparse(s); trans(starts(j):stops(j),starts(i)+1:stops(i)+1)=sparse(s'); end trans(:,starts(i))=([1;zeros(F-1,1)]); trans(stops(i),:)=([zeros(1,F-1),1]); end trans=sparse(trans);