parfor: error with feval
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
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
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
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
2018-12-22
initial_flag = 0;
if initial_flag==0
should be
if isempty(initial_flag)
without initializing it to 0 every time.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Parallel for-Loops (parfor) 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!