Can we use 'parfor' in user defined function?

5 visualizaciones (últimos 30 días)
Mohsin Shah
Mohsin Shah el 6 de Ag. de 2017
Comentada: Mohsin Shah el 7 de Ag. de 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?
  3 comentarios
Mohsin Shah
Mohsin Shah el 6 de Ag. de 2017
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 el 6 de Ag. de 2017
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)

Iniciar sesión para comentar.

Respuesta aceptada

Walter Roberson
Walter Roberson el 6 de Ag. de 2017
Yes, for sure you can, provided that you have the parallel computing toolbox installed and licensed
  4 comentarios
Walter Roberson
Walter Roberson el 7 de Ag. de 2017
function y = function_name(p)
t = 0;
parfor k = 1 : length(p)
t = t + p(k);
end
y = t;
end
Mohsin Shah
Mohsin Shah el 7 de Ag. de 2017
Thank you so much

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by