# Error using eigs. Input matrix is badly conditioned. It might contain Inf or NaN.

5 views (last 30 days)
Adam Levschuk on 23 Jun 2020
Commented: Adam Levschuk on 23 Jun 2020
Hello all,
I have a varibale named psi, that is a 1x10940 cell array of cells, with each cell containing a 2x2 matrix. I calcualted phi using the folloiwing code:
T = 60; %60 Hz is 1.2 seconds
phi = cell([],1) ;
count = 0 ;
for t = 1:length(gyr)-T
count = count+1 ;
phi{count} = [(n1(1,t+T)-n2(1,t+T)) / (n1(1,t)-n2(1,t)),...
(n3(1,t+T)-n4(1,t+T)) / (n3(2,t)-n4(2,t));...
(n1(2,t+T)-n2(2,t+T)) / (n1(1,t)-n2(1,t)),...
(n3(2,t+T)-n4(2,t+T)) / (n3(2,t)-n4(2,t))];
end
i then try to use the eigs function to find the largest eigen value for each matrix of phi using the code,
for i = 1:length(phi);
lambda_max(1,i) = eigs(phi{1,i},1);
end
However, i get an error message saying "Input matrix is badly conditioned. It might contain Inf or NaN."
I undertsand that this error is a result of the method by which im calculating phi. Does anyone have any suggestion on how to remdy this.
The input values for the phi calcualtion, n1,n2,n3, and n4 are all 2x11000 arrays of coordinate points where the top row is the X position and the bottom row is the Y position.
Thank you for any help or assistance you can offer.

Walter Roberson on 23 Jun 2020
find( cellfun(@(C) any(~isfinite(C(:))), psi) )
to locate entries with nan or inf.
I am confused that you are talking about psi but do not use that in your code.
Is there reason to believe that it is not possible for any of the subtractions in the denominators to be 0, leading to division by 0?
Adam Levschuk on 23 Jun 2020
I ended up figuring out what the probelm is and it was nothing i would have ever expected. Going to try and sort this issue out now.
Here it goes:
So i have a trajectory called "trajAP", it is an array of angular position and angular velocity. For each point in trajAP i define a point, p1, a certain distacne away from the reference point in trajAP. point p1 is in the positive x direction, point p2 is in the negative direction, point p3 is in the positive y direction, and point p4 is in the neagtive y direction.
I then use the function knnsearch to find the nearest neighbour of p1, p2, p3, and p4 that is on trajAP. I called these nearest neighbours n1,n2,n3, and n4, respectively.
the probelm came in regions of the trajectory where there are not lots of data points. Becuase sometimes the nearest penighbour was the same point and when i ended up calculating the matrix phi, i ended up dividing by 0 in some cases.
I now have to find a way to say if any of the n coordinates are the same then use the next nearest neighbour. Any ideas on how to do this?

R2020a

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!