Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Alternative solution for NAN
Date: Wed, 27 Feb 2013 12:56:08 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 72
Message-ID: <kgkvp8$4bi$1@newscl01ah.mathworks.com>
References: <kgki2g$rie$1@newscl01ah.mathworks.com> <kgklbh$6ph$1@newscl01ah.mathworks.com> <kgkmon$aeo$1@newscl01ah.mathworks.com> <kgko7b$e4d$1@newscl01ah.mathworks.com> <kgkpep$h9u$1@newscl01ah.mathworks.com> <kgktg9$rhc$1@newscl01ah.mathworks.com> <kgku2t$t2g$1@newscl01ah.mathworks.com> <kgkurb$1t8$1@newscl01ah.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: www-05-blr.mathworks.com
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: newscl01ah.mathworks.com 1361969768 4466 172.30.248.37 (27 Feb 2013 12:56:08 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Wed, 27 Feb 2013 12:56:08 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 3799640
Xref: news.mathworks.com comp.soft-sys.matlab:789903

"Carl  S." wrote in message <kgkurb$1t8$1@newscl01ah.mathworks.com>...
> "Torsten" wrote in message <kgku2t$t2g$1@newscl01ah.mathworks.com>...
> > "Carl  S." wrote in message <kgktg9$rhc$1@newscl01ah.mathworks.com>...
> > > "Carl  S." wrote in message <kgkpep$h9u$1@newscl01ah.mathworks.com>...
> > > > "Torsten" wrote in message <kgko7b$e4d$1@newscl01ah.mathworks.com>...
> > > > > "Carl  S." wrote in message <kgkmon$aeo$1@newscl01ah.mathworks.com>...
> > > > > > "Torsten" wrote in message <kgklbh$6ph$1@newscl01ah.mathworks.com>...
> > > > > > > "Carl  S." wrote in message <kgki2g$rie$1@newscl01ah.mathworks.com>...
> > > > > > > > The following code gives NAN (Not a Number) values
> > > > > > > > [U,D]=eig(N);  
> > > > > > > > 
> > > > > > > > To solve this problem, I wrote that
> > > > > > > > while(det(N) == 0)
> > > > > > > >      N=(1e-10.*randi(1,size(N)))*eye(size(N));
> > > > > > > > end
> > > > > > > > 
> > > > > > > > But, the loop does not stop
> > > > > > > 
> > > > > > > Your matrix N within the loop always has determinant (1e-10)^(size(N))
> > > > > > > which may become very small if N is large.
> > > > > > > 
> > > > > > >  :( Are there any alternative solution instead of this loop to solve the NAN problem ?
> > > > > > > 
> > > > > > > Depends on the original matrix N.
> > > > > > > 
> > > > > > > Best wishes
> > > > > > > Torsten.
> > > > > > 
> > > > > > Dear Torsten,
> > > > > > The matrix N has standard deviation values of grayscale images. So, it changes for each image. How to solve the NAN problem in this case ?
> > > > > 
> > > 
> > > I have tried this,
> > > u=1e-10;
> > > while(det(N) == 0)
> > >      N=u.*eye(size(N));
> > >      u=u*100;
> > > end
> > > 
> > > Now, it works without giving NAN value. But, I am not sure that this algorithm correct results. Do you think that this is meaningful or I can get unexpected results ? Any suggestions ?
> > 
> > The matrix N you get after the while loop is a scalar multiple of the identity matrix and in general has nothing in common with your original matrix N. 
> > You will have to find out why eig produces NaN values for your original matrix N. 
> > Are you sure all elements of N are finite ? 
> > 
> > Best wishes
> > Torsten.
> 
> Yes, Torsten, they are finite
> 
> My goal is to fit means(mu) and standard deviations(N) to Gaussian shape. The codes that I wrote above are from the function ;
> 
> function res=MultivariateGaussianPDF(x,mu,N)
> while(det(N) == 0)
>      N=(1e-10.*randi(1,size(N)))*eye(size(N));
> end
> 
> [M,d]=size(x);
> [U,D]=eig(N); % <=causes NAN problem   :((
>  
> W=sqrt(inv(D))*U;
> Wx=W*(x-ones(M,1)*mu)';
> res=(1/sqrt((2*pi)^d*det(N)))*exp(-0.5*sum(Wx.^2,1));

The while statement is complete nonsense.
The only thing I can imagine what is meant is
> while(det(N) == 0)
>      N=N+1e-10*eye(size(N));
> end

Best wishes
Torsten.