I presently have this code which tests a paricular function which I created _ _ [y n] function_gen1pdf1(a,b,c) _ _ for N times. Now this works fine up to N=100. However, I want it to work for N=100,000 but it's taking much too long to compute. Is it possible to vectorize it or make it work faster? Any suggestions are much appreciated!
N=100000;
y=zeros(N,1);
n=zeros(N,1);
for i=1:N; %testing the function for 100000 times
[y(i) n(i)]= function_gen1pdf1(2,3,0.5); %The function being tested
end

댓글 수: 7

Star Strider
Star Strider 2015년 2월 7일
Please post the function code!
Stephen23
Stephen23 2015년 2월 7일
"Is it possible to vectorize it": this depends entirely on the function function_gen1pdf1. If you want help trying to vectorize this, then you have to upload its code too.
rox - I don't understand the line
[y(i) n(i)]= function_gen1pdf1(2,3,0.5);
You keep calling function_gen1pdf1 with the same set of inputs for every i. Why is the code repeating the same call at every iteration of the for loop?
Tapan
Tapan 2015년 2월 7일
I will echo everyone else's comments - you need to provide the function code. Perhaps try a parlor loop in the meantime.
Jan
Jan 2015년 2월 7일
@Geoff: The "gen" in the name might mean a genetic algorithm, such that random effects can be the reasn for different results inspite of the same inputs.
Image Analyst
Image Analyst 2015년 2월 7일
Or maybe it generates a PDF document, or generates a probability distribution function. Whatever it does, apparently PDF does not mean "Pretty Darn Fast".
Jan
Jan 2015년 2월 7일
편집: Jan 2015년 2월 7일
@rox: While the "function_" in the name of your function is pretty meaningless, "gen1pdf1" catches our interest.

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

 채택된 답변

Image Analyst
Image Analyst 2015년 2월 7일

0 개 추천

It's your function, it's not the for loop. If you just run a for loop 100,000 times it takes 0.2 milliseconds.
I tried it:
tic
for k = 1 : 100000
;
end
toc
Elapsed time is 0.000219 seconds.
So it if takes a long time, it's due to your function_gen1pdf1() function.

댓글 수: 3

rox
rox 2015년 2월 7일
I guess the problem is that function_gen1pdf1(2,3,0.5) calls yet another function inside it (as it is based on the rejection method) which contains fplot. Now I was experimenting and just tried working it out without the fplot, and it's much much faster! Thanks for your clarifying that the problem was with the function itself!
Image Analyst
Image Analyst 2015년 2월 7일
rox, you're welcome. Does that mean that you can now mark the answer as "Accepted"?
rox
rox 2015년 2월 7일
Yes, thanks!

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

rox
2015년 2월 7일

편집:

Jan
2015년 2월 7일

Community Treasure Hunt

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

Start Hunting!

Translated by