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

cell array

I'm looking to store the value "p" (3×1 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

Best Answer

You have multiple iterations using an iterator i ... I suggest to use more descriptive variable names :D
Related Question