LU decomposition and updating vectors in a for loop

3 visualizaciones (últimos 30 días)
Daniel Villa
Daniel Villa el 19 de Abr. de 2018
Comentada: Daniel Villa el 19 de Abr. de 2018
I have a matrix A and vector b and I'm doing an LU decomposition 100 times in a for loop. For x=A\b I want to save the second term in x as a 1x100 row vector but I'm not sure how to make the vector update every time I go through the loop.

Respuestas (1)

John D'Errico
John D'Errico el 19 de Abr. de 2018
Editada: John D'Errico el 19 de Abr. de 2018

Does A stay fixed. Or does it vary each time in the loop? I cannot imagine why you are asking this question if A does not stay fixed, so I assume it does.

Do you have multiple vectors b? If so, why are you using a loop, when a simple matrix multiply will suffice?

Is this your own LU decomposition? WHY? Why not use the LU that MATLAB provides? Why are you using an LU at all for this? So is this homework?

For example, were I to want to solve this problem, I might do it like this:

A = rand(5,5); % random A, since I don't have your matrix A
b = rand(5,100); % 100 right hand sides
Ap = pinv(A);
Ap = Ap(2,:);
x2 = Ap*b;

x2 will be a 1x100 vector. Did it work? No reason to test all 100 elements. The first 3 should suffice.

A\b(:,1:3)
ans =
       -1.34762677238989         -2.32203158132578        -0.388109971448836
        3.72915190105665          1.83179475422785         0.820817371759009
       -2.49786105988984        0.0977008830984635        0.0936525938380136
       -0.27171174946352         0.178317044475116        -0.350094680576877
        -3.1181416137406         -1.19921562646932         0.279935332918383
x2(1:3)
ans =
        3.72915190105665          1.83179475422785          0.82081737175901

As you can see, x2(i) will be the second element of the solution vector, for the i'th column of the array b. And no loop is required.

So, do you really need to do this in a loop? Do you really need to use an LU?

  1 comentario
Daniel Villa
Daniel Villa el 19 de Abr. de 2018
Im sorry I should’ve been more clear. A is fixed and b is a 3x1 vector where the third row is changing from 1 to 100 (hence the for loop). So I want to save the second row in the resultant x vector for each time through.

Iniciar sesión para comentar.

Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by