Bad cell reference with cell array of function handles.
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Thales Silva
am 21 Aug. 2017
Kommentiert: Thales Silva
am 21 Aug. 2017
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 Kommentare
Akzeptierte Antwort
Walter Roberson
am 21 Aug. 2017
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 Kommentare
Guilherme Moraes
am 21 Aug. 2017
I had the same problem and this worked perfectly, thank you very much!
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Operators and Elementary Operations finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!