Please help with For Loop

1 visualización (últimos 30 días)
Raisul Islam
Raisul Islam el 6 de Nov. de 2017
Comentada: Raisul Islam el 7 de Nov. de 2017
u1 = v(:,1);
a2 = v(:,1)\v(:,2);
u2 = v(:,2) - v(:,1)*a2;
a3 = v(:,[1 2])\v(:,3);
u3 = v(:,3) - v(:,[1 2])*a3;
a4 = v(:,[1 2 3])\v(:,4);
u4 = v(:,4) - v(:,[1 2 3])*a4;
% Structural residuals and covariance
b02 = [ 1 0 0 0;
-a2(1) 1 0 0;
-a3(1) -a3(2) 1 0;
-a4(1) -a4(2) -a4(3) 1 ];
u = [u1 u2 u3 u4 ];
Please advise how can I estimate a , u and b with for loop...in case I have many more variables then just four.

Respuesta aceptada

Walter Roberson
Walter Roberson el 6 de Nov. de 2017
Index your variables instead of using fixed variable name. Use cell arrays.
a{K} = v(:,K-1)\v(:,K);
u{K} = v(:,K) - v(:,K-1)*a{K};
  6 comentarios
Walter Roberson
Walter Roberson el 6 de Nov. de 2017
Correction:
ncol = size(v,2);
u(:,1) = v(:,1);
a = zeros(ncol, ncol);
for K = 2 : size(v,2)
a(1:K-1,K) = v(:,1:K-1)\v(:,K);
u(:,K) = v(:,K) - v(:,1:K-1)*a(1:K-1,K);
end
b = diag(ones(1,ncol)) - tril(a.');
Raisul Islam
Raisul Islam el 7 de Nov. de 2017
thank you so much

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by