speed up 'for' loops
古いコメントを表示
Hi,
How can I speed up following 'for' loops? Help me please.
P=200;
N=40000000;
y(1:P)=3;
% a: P X 1 matrix (vector)
% Z: P X 1 matrix (vector)
% x: N X 1 matrix (vector)
%%%%%Loops
y(P+1:N)=0;
for i=P+1:N
for j=1:P
y(i)=y(i)-a(j)*x(i-j);
end
end
for i=1:N
for j=1:P
f(i,j)=Z(j)^(i-1);
end
end
Thanks in advance.
採用された回答
その他の回答 (1 件)
Azzi Abdelmalek
2012 年 9 月 7 日
for the second loop
c=repmat(Z',N,1)
f=bsxfun(@power,c,[0:N-1]')
4 件のコメント
Jan
2012 年 9 月 7 日
The power operation is surprisingly expensive. See "type vander".
Azzi Abdelmalek
2012 年 9 月 7 日
you are right, your code is faster for N=400000
Elapsed time is 0.489050 seconds.
Elapsed time is 0.757052 seconds. my code
Coo Boo
2012 年 9 月 7 日
Matt Fig
2012 年 9 月 7 日
No need for REPMAT or [].
f = bsxfun(@power,Z.',(0:N-1).');
カテゴリ
ヘルプ センター および File Exchange で Loops and Conditional Statements についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!