parfor: error with feval

2 次查看(过去 30 天)
Mahamed
Mahamed 2013-11-29
Hi all,
I wrote a the following script, which uses parfor:
N = 5;
M = 10;
x = rand(N,M);
f = zeros(1,N);
parfor j = 1:N
f(j) = func(x(j,:), fun, opt_f);
end
In "func", I call another function, "benchmark_func" where "feval" is used as follows:
f=feval(fhd,x)+f_bias(func_num);
I got the following error:
_Error using benchmark_func (line 76) Argument must contain a string or function_handle.
Error in func (line 22) fit = benchmark_func(x, fun);
Error in test_p (line 28) parfor j = 1:N
Caused by: Error using feval Argument must contain a string or function_handle._
I know we can't use "feval" inside parfor but is their a simple way to fix this.
Many thanks
Kind regards
Mahamed
  5 个评论
Mahamed
Mahamed 2013-11-30
编辑:Mahamed 2013-11-30
This is part of the benchmark_func code:
function f=benchmark_func(x,func_num)
global initial_flag
persistent fhd f_bias
if initial_flag==0
if func_num==1 fhd=str2func('sphere_func'); %[-100,100]
elseif func_num==2 fhd=str2func('schwefel_102'); %[-100,100]
...
elseif func_num==25 fhd=str2func('hybrid_rot_func4'); %[-5,5]
end
load fbias_data;
end
f=feval(fhd,x)+f_bias(func_num);
function fit=sphere_func(x)
global initial_flag
persistent o M
[ps,D]=size(x);
if initial_flag==0
load sphere_func_data
if length(o)>=D
o=o(1:D);
else
o=-100+200*rand(1,D);
end
initial_flag=1;
end
x=x-repmat(o,ps,1);
fit=sum(x.^2,2);
...
end
Edric Ellis
Edric Ellis 2013-12-2
You certainly can use FEVAL inside PARFOR - are you having trouble with that?
>> fh = {@sin, @cos, @tan};
>> parfor idx = 1:3, x(idx) = feval(fh{idx}, idx); end
>> x
x =
0.8415 -0.4161 -0.1425

请先登录,再进行评论。

回答(1 个)

Walter Roberson
Walter Roberson 2013-12-1
I would suggest a table such as
funhandles = {@sphere_func, @schwefel_102, .... @hybrid_rot_func4};
then
fhd = funhandle{func_num};
and then
f = fhd(x) + f_bias(func_num);
Notice feval() is gone.
  7 个评论
javeria barkat
javeria barkat 2018-12-22
编辑:javeria barkat 2018-12-22
Mohamed kindly tell me how u have solved the problem of "Caused by: Subscript indices must either be real positive integers or logicals" as statd above ? I have the same problem.
I am using same benchmark_func.m file
Walter Roberson
Walter Roberson 2018-12-22
initial_flag = 0;
if initial_flag==0
should be
if isempty(initial_flag)
without initializing it to 0 every time.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Parallel for-Loops (parfor) 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by