필터 지우기
필터 지우기

Saving Matricies from a Loop

조회 수: 2 (최근 30일)
Kyle Fertig
Kyle Fertig 2017년 12월 4일
답변: Mukul Rao 2017년 12월 6일
I'm creating a function to create an inverse matrix of some [A] following LU Factorization, and I have the correct loops to generate the columns of the inverse (defined by p), but the loop happens "n" times where n is the number of rows/columns in the square matrix. The problem is that "p" is overwritten each time the loop goes through, and I need to save the values from each time the loop happens so I can eventually piece the rows together into a final matrix: inverse A. I did some research and came across cells, but I was unsuccessful trying to implement it into my own code shown below. Any help is appreciated!
Thanks,
-Kyle
%%Taking Apart Identity Matrix
L=[1,0,0;2.56,1,0;5.76,3.5,1]
U=[25,5,1;0,-4.8,-1.56;0,0,0.7]
w=length(L)
I=eye(w)
b=[1;0;0]
n=length(L)
for i=1:w
b=I(:,i)
%%Gauss Elim L
n=length(L);
m=zeros(n,1);
x=zeros(n,1);
for k =1:n-1;
%compute the kth column of M
m(k+1:n) = L(k+1:n,k)/L(k,k);
%compute
%An=Mn*An-1;
%bn=Mn*bn-1;
for i=k+1:n
L(i, k+1:n) = L(i,k+1:n)-m(i)*L(k,k+1:n);
end;
b(k+1:n)=b(k+1:n)-b(k)*m(k+1:n);
end
W= triu(L);
%BACKWARD ELIMINATION
x(n)=b(n)/L(n,n);
for k =n-1:-1:1;
b(1:k)=b(1:k)-x(k+1)* W(1:k,k+1);
x(k)=b(k)/W(k,k)
end
%%Gauss Elim U
n=length(U);
m=zeros(n,1);
p=zeros(n,1);
for k =1:n-1;
%compute the kth column of M
m(k+1:n) = U(k+1:n,k)/U(k,k);
%compute
%An=Mn*An-1;
%bn=Mn*bn-1;
for i=k+1:n
U(i, k+1:n) = U(i,k+1:n)-m(i)*U(k,k+1:n);
end;
x(k+1:n)=x(k+1:n)-x(k)*m(k+1:n);
end
W= triu(U);
%BACKWARD ELIMINATION
p(n)=x(n)/U(n,n);
for k =n-1:-1:1;
x(1:k)=x(1:k)-p(k+1)* W(1:k,k+1);
p(k)=x(k)/W(k,k)
end
end

답변 (1개)

Mukul Rao
Mukul Rao 2017년 12월 6일
Hello, I made some minor changes. Note that you had reused "i" as a loop-variable inside a nested for-loop, I renamed these to "j" to not corrupt the value of "i" in the outermost loop. Finally, initializing p to a square matrix of zeros and replacing all p(k) s with p(k,i)s does the trick.
%%Taking Apart Identity Matrix
L=[1,0,0;2.56,1,0;5.76,3.5,1]
U=[25,5,1;0,-4.8,-1.56;0,0,0.7]
w=length(L)
I=eye(w)
b=[1;0;0]
n=length(L)
p = zeros(w,w);
for i=1:w
b=I(:,i)
%%Gauss Elim L
n=length(L);
m=zeros(n,1);
x=zeros(n,1);
for k =1:n-1;
%compute the kth column of M
m(k+1:n) = L(k+1:n,k)/L(k,k);
%compute
%An=Mn*An-1;
%bn=Mn*bn-1;
for j=k+1:n
L(j, k+1:n) = L(j,k+1:n)-m(j)*L(k,k+1:n);
end;
b(k+1:n)=b(k+1:n)-b(k)*m(k+1:n);
end
W= triu(L);
%BACKWARD ELIMINATION
x(n)=b(n)/L(n,n);
for k =n-1:-1:1;
b(1:k)=b(1:k)-x(k+1)* W(1:k,k+1);
x(k)=b(k)/W(k,k)
end
%%Gauss Elim U
n=length(U);
m=zeros(n,1);
for k =1:n-1;
%compute the kth column of M
m(k+1:n) = U(k+1:n,k)/U(k,k);
%compute
%An=Mn*An-1;
%bn=Mn*bn-1;
for j=k+1:n
U(j, k+1:n) = U(j,k+1:n)-m(j)*U(k,k+1:n);
end;
x(k+1:n)=x(k+1:n)-x(k)*m(k+1:n);
end
W= triu(U);
%BACKWARD ELIMINATION
p(n,i)=x(n)/U(n,n);
for k =n-1:-1:1
x(1:k)=x(1:k)-p(k+1,i)* W(1:k,k+1);
p(k,i)=x(k)/W(k,k);
end
end
>> p
p =
0.0476 -0.0833 0.0357
-0.9524 1.4167 -0.4643
4.5714 -5.0000 1.4286
>> (L*U)*p
ans =
1.0000 0 0
0.0000 1.0000 0.0000
0.0000 -0.0000 1.0000

카테고리

Help CenterFile Exchange에서 Graph and Network Algorithms에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by