function [sig, StdRes, chi] = chitest(C)	
	sig = 0;
	StdRes = zeros(size(C));
	
	ctotal = csum(C);
	rtotal = rsum(C);
	ck = find(ctotal);
	rk = find(rtotal);
	if ck==[] | rk==[]
		sig=1;
		chi=0;
		return
	end
	C = C(rk,ck);
	ctotal = ctotal(ck);
	rtotal = rtotal(rk);
	total  = sum(rtotal);
	EC = rtotal * ctotal  / total;
	chi = sum(sum((C-EC).^2./EC));
	df = prod(size(C)-1);
	sig = 1 - chi2cdf(chi, df);
	StdRes(rk,ck) = (C-EC)./sqrt(EC);
end