Can we use 'parfor' in user defined function?

7 次查看(过去 30 天)
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?
  3 个评论
Mohsin Shah
Mohsin Shah 2017-8-6
any user define function for example
% code
y=function_name(p);
% called function
function y=function_name(p)
parfor k=1:10
y=p+k
end
end
Walter Roberson
Walter Roberson 2017-8-6
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)

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2017-8-6
Yes, for sure you can, provided that you have the parallel computing toolbox installed and licensed
  4 个评论
Walter Roberson
Walter Roberson 2017-8-7
function y = function_name(p)
t = 0;
parfor k = 1 : length(p)
t = t + p(k);
end
y = t;
end

请先登录,再进行评论。

更多回答(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