Can we use 'parfor' in user defined function?

조회 수: 7 (최근 30일)
Mohsin Shah
Mohsin Shah 2017년 8월 6일
댓글: Mohsin Shah 2017년 8월 7일
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
Mohsin Shah
Mohsin Shah 2017년 8월 7일
Thank you so much

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

추가 답변 (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