speed for loop in sum

조회 수: 1 (최근 30일)
Ole
Ole 2019년 3월 11일
댓글: Ole 2019년 3월 11일
Can the for loop be removed ?
x=linspace(-2,2,100); %some vector
[X,XX]=meshgrid(x,x); %some matrix
a=1/2;%some constant
A0=zeros(size(X));%initialize the sum
A0k = @(k,X,a)(((-1)^k).*((X./a).^(2*k)).*besselj(2*k,X));%series
for l=0:20
A0 = A0+A0k(l,X,a); %sum of series
end
  댓글 수: 2
Torsten
Torsten 2019년 3월 11일
편집: Torsten 2019년 3월 11일
What's the problem with the solution I gave ?
Ole
Ole 2019년 3월 11일
I was not able to make it work.

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

채택된 답변

Torsten
Torsten 2019년 3월 11일
function main
x = linspace(-2,2,100); %some vector
[X,XX] = meshgrid(x,x); %some matrix
k = 0:20;
a = 0.5;
A0 = arrayfun(@(x)sum((-1).^k.*(x/a).^(2*k).*besselj(2*k,x)),X)
end
  댓글 수: 1
Ole
Ole 2019년 3월 11일
Thank you!
just as feedback the arrayfun is slower than the loop.
0.096s for the for loop
0.30s for the arrayfun

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by