Hi,
Is there a way to speed up this?
maxN = 120;
x = -30:0.1:30;
xElements = numel(x);
u_mn = zeros(xElements, xElements);
for m = -maxN:2:maxN
for i = 1 : xElements
for j = 1 : xElements
u_mn(i, j) = sqrt((n+1)/pi) * besselj(m+1, 2*sqrt(x(i)^2 + x(j)^2)) / sqrt(x(i)^2 + x(j)^2)^(m+1) * (x(i) + 1i*x(j))^m;
end
end
end
Best regards, Alex

댓글 수: 4

CS Researcher
CS Researcher 2016년 6월 1일
편집: CS Researcher 2016년 6월 1일
What is n here? Is there another loop for it?
Im sorry, youre right, there is. The entire code is:
x = -30:0.1:30;
xElements = numel(x);
maxN = 120;
parfor n = 0:maxN
u_mn(:, :, n+1) = firstTestFunct(maxN, xElements, x, n);
end
u_mnSummed = sum(u_mn, 3);
and the function:
function u_mn = firstTestFunct(maxN, xElements, x, n)
u_mn = zeros(xElements, xElements);
for m = -maxN:2:maxN
for i = 1 : xElements
for j = 1 : xElements
u_mn(i, j) = sqrt((n+1)/pi) * besselj(m+1, 2*sqrt(x(i)^2 + x(j)^2)) / sqrt(x(i)^2 + x(j)^2)^(m+1) * (x(i) + 1i*x(j))^m;
end
end
end
This is the fastest version I have now.
Walter Roberson
Walter Roberson 2016년 6월 1일
You do not store the results for each different m, and you are not summing them or anything like that, so the effect is as if you had only done the final m value.
Thanky you, obviously, you are right. the middle line in the loops should look like this:
for m = -maxN:2:maxN
for i = 1 : xElements
for j = 1 : xElements
u_mn(i, j) = u_mn(i, j) + sqrt((n+1)/pi) * besselj(m+1, 2*sqrt(x(i)^2 + x(j)^2)) / sqrt(x(i)^2 + x(j)^2)^(m+1) * (x(i) + 1i*x(j))^m;
end
end
end
But still, how to speed this up? Its not working to fast right now.

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

 채택된 답변

Ahmed Rashid
Ahmed Rashid 2016년 6월 2일

0 개 추천

You can create a mex function of your firstTestFunct by
xx = 1;
codegen firstTestFunct -args {xx, xx, xx, xx}
You need to do it only once. It will generate a mex file. To call the generated mex file, you just need to replace firstTestFunct with firstTestFunct_mex .

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Operators and Elementary Operations에 대해 자세히 알아보기

제품

질문:

2016년 6월 1일

답변:

2016년 6월 2일

Community Treasure Hunt

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

Start Hunting!

Translated by