Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Alternative solution for NAN

Subject: Alternative solution for NAN

From: Carl S.

Date: 27 Feb, 2013 09:02:09

Message: 1 of 12

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 :( Are there any alternative solution instead of this loop to solve the NAN problem ?

Subject: Alternative solution for NAN

From: Torsten

Date: 27 Feb, 2013 09:58:09

Message: 2 of 12

"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.

Subject: Alternative solution for NAN

From: Carl S.

Date: 27 Feb, 2013 10:22:15

Message: 3 of 12

"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 ?

Subject: Alternative solution for NAN

From: Torsten

Date: 27 Feb, 2013 10:47:07

Message: 4 of 12

"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 ?

Did you check whether the matrix N already contains NaN values ?

Best wishes
Torsten.

Subject: Alternative solution for NAN

From: Carl S.

Date: 27 Feb, 2013 11:08:09

Message: 5 of 12

"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 ?
>
> Did you check whether the matrix N already contains NaN values ?
>
> Best wishes
> Torsten.

Yes, I have checked with this codes

if isnan(N)==1
h=1;
else
h=0;
end

This code return that h=0

Subject: Alternative solution for NAN

From: Carl S.

Date: 27 Feb, 2013 11:17:08

Message: 6 of 12

"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 ?
> >
> > Did you check whether the matrix N already contains NaN values ?
> >
> > Best wishes
> > Torsten.
>
> Yes, I have checked with this codes
>
> if isnan(N)==1
> h=1;
> else
> h=0;
> end
>
> This code return that h=0
Dear Torsten ,
I also wrote this;
checkNAN=sum(find(isnan(N)==1))
The value of checkNAN is zero :(

Subject: Alternative solution for NAN

From: Carl S.

Date: 27 Feb, 2013 12:17:13

Message: 7 of 12

"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 ?

Subject: Alternative solution for NAN

From: Torsten

Date: 27 Feb, 2013 12:27:09

Message: 8 of 12

"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.

Subject: Alternative solution for NAN

From: Carl S.

Date: 27 Feb, 2013 12:40:11

Message: 9 of 12

"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));

Subject: Alternative solution for NAN

From: Torsten

Date: 27 Feb, 2013 12:56:08

Message: 10 of 12

"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.

Subject: Alternative solution for NAN

From: Steven_Lord

Date: 27 Feb, 2013 16:47:12

Message: 11 of 12



"Carl S." <tkittler@gmail.com> wrote in message
news:kgkurb$1t8$1@newscl01ah.mathworks.com...
> "Torsten" wrote in message <kgku2t$t2g$1@newscl01ah.mathworks.com>...

*snip*

>> 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)

1) Don't use DET to test for singularity. This matrix:

A = 1e-10*eye(400);

has determinant 0 (due to underflow) but it's a scaled identity matrix,
which is about as well-behaved as you can get. If you _must_ test for
singularity, check with COND or RCOND.

2) Don't test a floating-point number for exact, bit-for-bit equality unless
you need exact, bit-for-bit equality. Compare with a tolerance instead.

> N=(1e-10.*randi(1,size(N)))*eye(size(N));
> end
>
> [M,d]=size(x);
> [U,D]=eig(N); % <=causes NAN problem :((

Show the group a SMALL matrix N with which you can reproduce this behavior.

*snip*

3) If you have Statistics Toolbox, do one of these functions do what you
want?

http://www.mathworks.com/help/stats/multivariate-normal-distribution-1.html

http://www.mathworks.com/help/stats/normal-distribution-1.html

If not, please explain more _in words not code or equations_ specifically
what you mean/are trying to do when you say you want to fit means and
standard deviations to a Gaussian shape.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: Alternative solution for NAN

From: Carl S.

Date: 27 Feb, 2013 22:44:06

Message: 12 of 12

"Steven_Lord" <slord@mathworks.com> wrote in message <kgldag$ir9$1@newscl01ah.mathworks.com>...
>
>
> "Carl S." <tkittler@gmail.com> wrote in message
> news:kgkurb$1t8$1@newscl01ah.mathworks.com...
> > "Torsten" wrote in message <kgku2t$t2g$1@newscl01ah.mathworks.com>...
>
> *snip*
>
> >> 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)
>
> 1) Don't use DET to test for singularity. This matrix:
>
> A = 1e-10*eye(400);
>
> has determinant 0 (due to underflow) but it's a scaled identity matrix,
> which is about as well-behaved as you can get. If you _must_ test for
> singularity, check with COND or RCOND.
>
> 2) Don't test a floating-point number for exact, bit-for-bit equality unless
> you need exact, bit-for-bit equality. Compare with a tolerance instead.
>
> > N=(1e-10.*randi(1,size(N)))*eye(size(N));
> > end
> >
> > [M,d]=size(x);
> > [U,D]=eig(N); % <=causes NAN problem :((
>
> Show the group a SMALL matrix N with which you can reproduce this behavior.
>
> *snip*
>
> 3) If you have Statistics Toolbox, do one of these functions do what you
> want?
>
> http://www.mathworks.com/help/stats/multivariate-normal-distribution-1.html
>
> http://www.mathworks.com/help/stats/normal-distribution-1.html
>
> If not, please explain more _in words not code or equations_ specifically
> what you mean/are trying to do when you say you want to fit means and
> standard deviations to a Gaussian shape.
>
> --
> Steve Lord
> slord@mathworks.com
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com

Thanks Steven and Torsten,
(It is difficult to prepare a small group of matrix N. I am trying to find likelihood of an image. Codes are working for many images, but gives this NAN error for some of them)
I examined all codes again and saw that the error is due to the non-positive definite values of N. So, I wrote that

[U,D]= chol(N);
if D ~= 0 % <= if N not positive definite
    positivedefiniteN = topdm(N); %<=topdm converts N to a positive definite matrix
    [U,D]= chol(positivedefiniteN );
end

(For topdm function, the link is http://www.mathworks.com/matlabcentral/fileexchange/35938-converts-a-non-positive-definite-symmetric-matrix-to-positive-definite-symmetric-matrix/content/topdm.m)

But, the value of D returns zero therefore
W=sqrt(inv(D))*U;
returns NAN and inf values, since, inv(D)=inf
:((

I have tried this
    nonsingularD = pinv(D);
but nonsingularD is equal to zero, so it does not solve this problem

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us