speed up 'for' loops
1 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
Jan
2012-9-7
编辑:Jan
2012-9-7
P = 200;
N = 40000000;
a = rand(P, 1);
Z = rand(P, 1);
x = rand(N, 1);
y(P+1:N) = 0; % This one at first! -> pre-allocation
y(1:P) = 3;
at = transpose(a);
for i=P+1:N
y(i) = y(i) - at * x(i-1:-1:i-P); % Dot-product of vectors => SUM
end
f = ones(P, N);
for i = 2:N
f(:, i) = f(:, i - 1) .* Z;
end
f = transpose(f);
5 个评论
更多回答(1 个)
另请参阅
类别
在 Help Center 和 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!