Using Cell Arrays to Store a Matrix Each Time Through a Loop
1 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
Jos (10584)
2017-12-4
You have multiple iterations using an iterator i ... I suggest to use more descriptive variable names :D
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!