MATLAB Answers


Can we use 'parfor' in user defined function?

Asked by Mohsin Shah on 6 Aug 2017
Latest activity Commented on by Mohsin Shah on 7 Aug 2017
Hello, Can we use 'parfor' in used defined functions? I want to use parfor in user defined functions to improve the speed of my code but its not working. Is there any way we can use it?


on 6 Aug 2017
What function you are trying to use parfor?
any user define function for example
% code
% called function
function y=function_name(p)
parfor k=1:10
That use of parfor is not permitted because the output would depend upon which iteration ran last. You need either assign to an indexed variable or you need to use a reduction variable (for example totaling the values of the individual iterations)

Sign in to comment.

1 Answer

Answer by Walter Roberson
on 6 Aug 2017
 Accepted Answer

Yes, for sure you can, provided that you have the parallel computing toolbox installed and licensed


Show 1 older comment
I have parallel computing toolbox installed but still its not working, may be I don't know how to handle parfor in called functions. Would you give me a simple example of a code?
function y = function_name(p)
t = 0;
parfor k = 1 : length(p)
t = t + p(k);
y = t;
Thank you so much

Sign in to comment.