Vectorization of a loop that uses past values

1 回表示 (過去 30 日間)
Berkay Güler
Berkay Güler 2020 年 7 月 20 日
回答済み: Dinesh Yadav 2020 年 7 月 23 日
Is it possible to vectorize a loop if it uses past values? When iterations have a large number, simulation takes time to finish which i want to avoid. But i couldn't understand how to vectorize a loop when it has a past value.
N=100;
iterations=20000;
x=randn(1,N);
x=[x zeros(1,iterations-N)];
out=0;
sig=zeros(1,length(x)+1);
y=zeros(1,N+1);
for i=1:iterations
out=x(i) +0.5*(y(N)+y(N+1));
y=[out, y(1:length(y)-1)];
sig(i) =out;
end

採用された回答

Dinesh Yadav
Dinesh Yadav 2020 年 7 月 23 日
You cannot vectorize the above code as you have cyclic dependency in your code.

その他の回答 (0 件)

カテゴリ

Help Center および 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