¿Cómo podría solucionar matrices por el comando [L,U]? no sé nada respecto al tema
Show older comments
Tengo una matriz que no puedo solucioanr correctamente usando el comando [L,U] y no estoy seguro de como arreglarlo, solo buscaba la descomposición
3 Comments
Walter Roberson
on 23 May 2026
Approximate translation:
How can I solve matrices using the [L,U] command? I know nothing about this.
I have a matrix that I can't solve correctly using the [L,U] command, and I'm not sure how to fix it. I was just looking for the decomposition.
Walter Roberson
on 23 May 2026
I find it difficult to create a square matrix that lu() obviously fails on, such as creating NaN or inf.
Torsten
on 23 May 2026
Could you provide the matrix you have problems with ?
Answers (1)
John D'Errico
on 23 May 2026
Edited: John D'Errico
on 26 May 2026
I think your confusion stems from a misunderstanding. LU is fine, even with rank deficient matrices. As long as you use the MATLAB LU code, not your own, hand written code. And if you use your own code, we cannot possibly diagnose code we do not see. It is what you will do with the LU factors that matters, and that is where any problems will arise.
For example, the most severely rank deficient matrix is just the zeros matrix. This is about as bad as it can possibly get.
A0 = zeros(3);
[L0,U0] = lu(A0)
And LU survived with no complaints, no INFs, no NaNs, not even a warning. And for good reason. The L and U factors returned do indeed solve the problem as posed, since they allow you to reconstruct A0, and they have the necessary form.
L0*U0
Or a matrix of all ones.
A1 = ones(3);
[L1,U1]= lu(A1)
Again, L1 and U1 are sufficient to create A1, they have the requisite forms, and that is all that is required. But no probles were observed.
Even a matrix which is not as boring as the ones above, but is known to be singular is easily handled:
A2 = magic(4)
[L2,U2] = lu(A2)
L2*U2
As you see, we can easily reconstruct the rank deficient starting matrix, even though A2 was deficient itself.
rank(A2)
Rank deficiencies in any form are absolutely no problem.
Yes, if there is a NaN or an INF in your matrix already, now you should expect to see an issue.
A3 = rand(3,2)*rand(2,3); A3(2,2) = inf
[L3,U3] = lu(A3)
An inf appears, due to the presence of an INF already there.
If I had put a NaN in there, as you can see, NaNs result. And NaNs tend to propagate even more than INFs.
A4 = A3;A4(2,2) = NaN;
[L4,U4] = lu(A4)
But overall, LU is pretty tolerant, unless you throw trash at it that already has a NaN or an INF. NaNs are like wire coat hangers. They breed forever, and cannot be exterminated easily once you get one.
Yet for a simple matrix that lacks any singular elements, you will get a result that lacks NaNs or infs. The L and U factors will sometimes be problematic for future operations, but by themselves until you try to use them, expect no problems. It is only when we do something like then use the result of LU to try to solve a linear system of equations...
b = rand(3,1)
x = U0\(L0\b)
And now we see INFs and NaNs appear. That is completely expected, because now you need to use division. LU essentially never has a problem a long as you do not pass in NaNs or INFs. The problem occurs because you only ever perform an LU factorization is to do something at least the equivalent of solving a system of equations. Then the NaNs and INFs can arise.
Edit: Let me add one additional point. The classic LU factorization you will find in much of the pseudo-code available, that will try to perform a divide by pivot elements. And it is the divides which will cause problems, because at some point when you have rank deficiencies, you need to worry about a divide by zero, or something that is effectively zero to within floating point trash. And that could create a problem in less carefully written code. For example, in here:
We see MATLAB code for a very basic unpivoted LU. And in there, you will see a divide by the pivot elements.
function LU = LUDecompDoolittle(A)
n = length(A);
LU = A;
for k = 2:n
for i = 1 : k-1
lamda = LU(k,i) / LU (i, i);
LU(k,i) = lamda;
LU(k,i+1:n) = LU(k,i+1:n) - LU(i,i+1:n) * lamda;
end
end
end
From that code, we do see NaNs introduced..
LUDecompDoolittle(zeros(3))
Perhaps this is the target of your question. But you should realize that MATLAB's LU code is more careful, and is able to provide LU factors which are valid even on more problematic matrices. (This is why you use a tool like MATLAB, to gain access to professionally written code which is robust and stable.) Again, you will still see issues when you try to use those factors to solve systems of equations. But that MUST be an issue at some point on rank deficient systems. NaNs are a symptom of ill-posed problems, a suggestion of undecidability, of inadequate information.
Categories
Find more on Solver Outputs and Iterative Display 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!