%% sparse auto-associative network
clear; clf; hold on;
nn=500; a=0.1; npat=40;
for irun=1:10;
    pat=zeros(nn,npat);
    for i=1:npat; idx=randperm(nn); pat(idx(1:a*nn),i)=1; end
    w=(pat-a)*(pat-a)'; w=w/sqrt(npat); ic=0;
    for c=0:0.01:0.2; ic=ic+1; cc(ic)=c;
        s=pat(:,1); s(1:10)=1-s(1:10);
        for t=1:10; s=((w-c)*s)>0; end   % Update network
        aret(ic,irun)=sum(s)/nn; 
        hd(ic,irun)=((1-s)'*pat(:,1)+s'*(1-pat(:,1)))/nn;
    end
end
errorbar(cc,mean(aret'),std(aret')); 
errorbar(cc,mean(hd'),std(hd'));
