speed up 'for' loops

조회 수: 4 (최근 30일)
Coo Boo
Coo Boo 2012년 9월 7일
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.

채택된 답변

Jan
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
Jan
Jan 2012년 9월 7일
편집: Jan 2012년 9월 7일
Thanks, Coo Boo, fixed now. Unfortunately I cannot test it, because I do not have a Matlab version installed on my current computer. But you are cordially invited to debug it.
Coo Boo
Coo Boo 2012년 9월 7일
Thank you very much

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Azzi Abdelmalek
Azzi Abdelmalek 2012년 9월 7일
for the second loop
c=repmat(Z',N,1)
f=bsxfun(@power,c,[0:N-1]')
  댓글 수: 4
Coo Boo
Coo Boo 2012년 9월 7일
Thank you very much
Matt Fig
Matt Fig 2012년 9월 7일
No need for REPMAT or [].
f = bsxfun(@power,Z.',(0:N-1).');

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by