필터 지우기
필터 지우기

Using Cell Arrays to Store a Matrix Each Time Through a Loop

조회 수: 3 (최근 30일)
Kyle Fertig
Kyle Fertig 2017년 12월 4일
댓글: Kyle Fertig 2017년 12월 4일
I'm looking to store the value "p" (3x1 matrix) found each time going through a loop. In my code below, I attempt to use a cell array through the line "M{i}=p" where i is the value increasing by 1 each time I go through the loop. What I'm finding is that the cell is made, but instead of putting the second loop in the second cell, it just overwrites p in the same first cell. I appreciate any help in fixing this!
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)
M=cell(1,n)
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
M{i}=p
end

채택된 답변

Jos (10584)
Jos (10584) 2017년 12월 4일
You have multiple iterations using an iterator i ... I suggest to use more descriptive variable names :D
  댓글 수: 1
Kyle Fertig
Kyle Fertig 2017년 12월 4일
THANK YOU SO MUCH! Being totally honest, this code is a combination of a bunch of little things I've written and stuff I've found on the internet. I didn't even notice i was redefined later in the code.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by