I'm having a bad time figuring out what's wrong with this code:
dk = [1;1];
xmin = [1;1];
f = @(x1,x2)(x1 + x2);
aux = cell(size(xmin));
for i = 1 : length(xmin)
aux{i} = @(a)(xmin(i)+a*dk(i));
end
fa = @(a) f(aux{:}(a));
Given a multivariable function "f", when I try to evaluate "fa" at a given "a" (fa(1)), I get the following error: "Bad cell reference operation". Everything works perfectly by doing
fa = @(a) f(aux{1}(a), aux{2}(a));
but my application must work for an unknown number of variables of f.

2 件のコメント

KSSV
KSSV 2017 年 8 月 21 日
What version you are on? It is working in 2017(a)
Thales Silva
Thales Silva 2017 年 8 月 21 日
編集済み: Thales Silva 2017 年 8 月 21 日
The loop-variable is substituted and dk was previously defined. You can check it out by writing
fa = @(a) f(aux{1}(a), aux{2}(a));
and evaluating
f(1)
I'm using R2013a.

サインインしてコメントする。

 採用された回答

Walter Roberson
Walter Roberson 2017 年 8 月 21 日

1 投票

It is not legal to use () indexing with an unknown number of arguments produced by a cell array expansion. Also, MATLAB has no compact syntax for executing a number of functions with the same argument.
f_cell = @(args_cell) f(args_cell{:}); %utility anonymous function
fa = @(a) f_cell( cellfun( @(F) F(a), aux, 'uniform', 0) );

2 件のコメント

Guilherme Moraes
Guilherme Moraes 2017 年 8 月 21 日
I had the same problem and this worked perfectly, thank you very much!
Thales Silva
Thales Silva 2017 年 8 月 21 日
It works!!! God bless smart people... Thanks a lot...

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File Exchange で Creating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by