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.

 採用された回答

Walter Roberson
Walter Roberson 2017 年 11 月 6 日

0 投票

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 件のコメント

Raisul Islam
Raisul Islam 2017 年 11 月 6 日
Are you sure Walter? Each v is a 5000 by 1 vector. And how will I create the lower triangular matrix with loop?
Raisul Islam
Raisul Islam 2017 年 11 月 6 日
OK walter. for a{k} its fine, because a returns scaler. for 32 column that's fine. But for u , its not fine. The loop returns scaler. Where the loop is supposed to return vectors of size m by n. Note, v is sized m by n.
Walter Roberson
Walter Roberson 2017 年 11 月 6 日
u(:,1) = v(:,1);
for K = 2 : size(v,2)
a(K) = v(:,K-1)\v(:,K);
u(:,K) = v(:,K) - v(:,K-1)*a(K);
end
Raisul Islam
Raisul Islam 2017 年 11 月 6 日
Thank you so much. That worked easily. Can you help me with the final part. How can I make the lower triangular matrix with a loop? Thnx
Walter Roberson
Walter Roberson 2017 年 11 月 6 日
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 2017 年 11 月 7 日
thank you so much

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by