¿Cómo podría solucionar matrices por el comando [L,U]? no sé nada respecto al tema

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

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.
I find it difficult to create a square matrix that lu() obviously fails on, such as creating NaN or inf.
Could you provide the matrix you have problems with ?

Sign in to comment.

Answers (1)

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)
L0 = 3×3
1 0 0 0 1 0 0 0 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
U0 = 3×3
0 0 0 0 0 0 0 0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
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
ans = 3×3
0 0 0 0 0 0 0 0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Or a matrix of all ones.
A1 = ones(3);
[L1,U1]= lu(A1)
L1 = 3×3
1 0 0 1 1 0 1 0 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
U1 = 3×3
1 1 1 0 0 0 0 0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
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)
A2 = 4×4
16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
[L2,U2] = lu(A2)
L2 = 4×4
1.0000 0 0 0 0.3125 0.7685 1.0000 0 0.5625 0.4352 1.0000 1.0000 0.2500 1.0000 0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
U2 = 4×4
16.0000 2.0000 3.0000 13.0000 0 13.5000 14.2500 -2.2500 0 0 -1.8889 5.6667 0 0 0 0.0000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
L2*U2
ans = 4×4
16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
As you see, we can easily reconstruct the rank deficient starting matrix, even though A2 was deficient itself.
rank(A2)
ans = 3
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
A3 = 3×3
0.0964 0.1889 0.0423 0.7268 Inf 0.1187 0.6828 0.7790 0.1176
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
[L3,U3] = lu(A3)
L3 = 3×3
0.1326 1.0000 0 1.0000 0 0 0.9395 NaN 1.0000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
U3 = 3×3
0.7268 Inf 0.1187 0 -Inf 0.0266 0 0 NaN
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
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)
L4 = 3×3
0.1326 1.0000 0 1.0000 0 0 0.9395 NaN 1.0000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
U4 = 3×3
0.7268 NaN 0.1187 0 NaN 0.0266 0 0 NaN
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
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)
b = 3×1
0.8923 0.6625 0.1397
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
x = U0\(L0\b)
Warning: Matrix is singular to working precision.
x = 3×1
Inf NaN NaN
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
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))
ans = 3×3
0 0 0 NaN NaN NaN NaN NaN NaN
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
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.

Tags

Asked:

on 23 May 2026

Edited:

on 26 May 2026

Community Treasure Hunt

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

Start Hunting!