MATLAB: Merging Function Handles -
i'm coding simulation in matlab , need in regards issue i've been having.
i'm working on problem have n
separate anonymous function handles f_i
, each of stored in cell array functions
, accepts 1x1 numeric array x_i
, returns 1x1 numeric array y_i
.
i'm trying combine each of these anonymous function handles single anonymous function handle accepts single n
x1
numeric array x
, returns single n
x1
-numeric array y
. here, x(i) = x_i
, y(i) = y_i = f_i(x_i)
as example let n = 2
, f_1 , f_2 2 function handles input , output 1x1
arrays , stored in cell array named functions
f_1 = @(x_1) x_1^2 f_2 = @(x_2) x_2^3 functions = {f_1,f_2}
i need code able use n
, f_1
, f_2
construct function handle f inputs , outputs 2x1
numeric array.
f = @(x) [f_1(x(1,1));f_2(x(2,1))]
it's difficult define such function using inline @()
-anonymous syntax (because of limiting requirement on function's body expression). still it's possible define ordinary (non-anonymous) function runs on items of given vector , applies functions given cell array items.
function y = apply_funcs(f, x) assert(length(f) == length(x)); y = x; = 1 : length(f) y(i) = feval(f{i}, x(i)); end end
and every time it's needed pass function other one, reference @
-handle.
f = @apply_funcs
Comments
Post a Comment