How to set up a repeating series of terms
古いコメントを表示
Hi all
For the below code, I'm trying to set up a series where instead of just the most recent value of k, it increases the size of the matrix every time it runs, so it would be Xt = [ones(t-h+1,1), Lt*yt, Lt^2*yt, ... Lt^k*yt]
for k = 1:p
Xt = [ones(t-h+1,1), Lt^k*yt];
end
I know this code will only use the most recent value of k, so how would I be able to create it the way I described before? I'm pretty new to coding in general so apologies if this is a silly question.
3 件のコメント
You could do it with a loop, but it's unnecessary and generally slower.
Lt = 2;
t = 100;
h = 5;
p = 50;
yt = rand(t-h+1,1);
% using a loop without preallocation
tic
xt = ones(t-h+1,1);
for k = 1:p
xt = [xt Lt^k*yt];
end
toc
% using a loop with preallocation
tic
xt2 = ones(t-h+1,p+1);
for k = 1:p
xt2(:,k+1) = Lt^k*yt;
end
toc
% vectorized instead (using implicit array expansion)
tic
xt3 = [ones(t-h+1,1) Lt.^(1:p).*yt];
toc
% show that results match
immse(xt,xt2)
immse(xt,xt3)
Bethany Bloomfield
2021 年 11 月 1 日
DGM
2021 年 11 月 1 日
I don't know what size any of these are, nor what the goal is.
回答 (1 件)
Prateek Rai
2021 年 11 月 6 日
To my understanding, you want to set up a repeating series of terms so that,
Xt = [ones(t-h+1,1), Lt*yt, Lt^2*yt, ... Lt^k*yt].
Here is a possible workaround:
cat = [];
for k = 1 : p
cat = [cat, Lt^k*yt];
Xt = [ones(t-h+1,1), cat];
end
カテゴリ
ヘルプ センター および File Exchange で Mathematics についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!