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

2 vues (au cours des 30 derniers jours)
Kyle Fertig
Kyle Fertig le 4 Déc 2017
Commenté : Kyle Fertig le 4 Déc 2017
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

Réponse acceptée

Jos (10584)
Jos (10584) le 4 Déc 2017
You have multiple iterations using an iterator i ... I suggest to use more descriptive variable names :D
  1 commentaire
Kyle Fertig
Kyle Fertig le 4 Déc 2017
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.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Operators and Elementary Operations dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by