function pvalStouffer=pval_Stouffer(pval,u)
%input
%pval:  a Vxn matrix of contrast p-values
pvalStouffer =NaN*ones(size(pval,1),1);
pval(pval==0) = 1.0000e-016;
for v=1:size(pval,1)
    p=squeeze(pval(v,:));
    n = sum(~isnan(p));
    if n>(u-1)
        p=sort(p(~isnan(p)));
        zscores = sort(norminv(1-p));
        teststat = sum(zscores(1:n-u+1))/ sqrt(n -u+1);
        pvalStouffer(v) = 1-normcdf(teststat);
    end
end;