function chi2plotnew(X)

[n,p]=size(X);

S=cov(X);    m = mean(X);

for k=1:n,
   d(k,1) = ( X(k,:)-m)*(S\(X(k,:)-m)');
end
[d,ix]=sort(d);
j=(1:n)';
z=chi2inv( (j-0.5)/n,p);
qqplot(z,d);
xlabel('\chi^2_p');
ylabel('distances');
hold on
text(z,d,int2str(ix));
hold off


