how to avoid nan result when calculate sparse matrics division

9 views (last 30 days)
I'm doing a calculation using sparse matrics with the size of 12905*12905. I attached the code here.
WH = ones(P.npts, 1);
A = [L.*WL;sparse(1:P.npts,1:P.npts, WH)];
b = [zeros(P.npts,3);sparse(1:P.npts,1:P.npts, WH)*P.pts];
cpts = (A'*A)\(A'*b);
'WL' is a weighted value. 'P.npts' means the number of points with double type, which is a mumber in the structure 'P'.
P.npts = 12905
My question is: Sparse matric 'A' and 'b'are not a zero matrics, and matric (A'*A) either. But I always get 'NaN' result of cpts matrics. How to avoid getting NaN result when calculating a sparse matric. The structure rank of A is 12905. Could anyone help to solve this problem? Really appreciated!
Thanks

Accepted Answer

John D'Errico
John D'Errico on 31 Aug 2017
Sigh. Without seeing your numbers, this is hard to tell. Just telling us the size of the matrix is irrelevant.
I will tell you that you are doing something that is foolish.
Do NOT do this:
cpts = (A'*A)\(A'*b);
That squares the condition number of the problem.
Whereas this does not:
cpts = A\b;
Whoever told you to use the form you are using was steering you to a very poor way of solving the problem.
In the end, what matters is what the rank and condition number of A is in terms of linear algebra. So what does this tell you?
cond(A)
If that is too slow, then just do
condest(A)
It won't be to far off. If the condition number is greater than roughly 1e8, then the form you were using will have a condition number of roughly 1e16, which will then potentially yield NaNs for a result.
So at the very least, report the condition number of A.
  2 Comments
Heather Zhang
Heather Zhang on 1 Sep 2017
Thank you very much, John, for your advise. The size of matric b is 25810*3. The size of sparse matric A is 25810*12905 which is not a square matrics. Could you tell me how to estimate the condition number of A? Rearlly appreciate it. The condition number of (A'*A) is inf. Thanks

Sign in to comment.

More Answers (0)

Categories

Find more on Sparse Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!