I've searched and there are many answers to this kind of question, suggesting functions like arrayfun, bsxfun, and so on. I haven't been able to resolve the issue due to dimension mismatches (and probably a fundamental misunderstanding as to how MATLAB treats anonymous function handles).
I have a generic function handle of more than one variable:
f = @(x,y) (some function of x, y)
Heuristically, I would like to define a new function handle like
g = @(x) sum(f(x,1:3))
More precisely, the following does exactly what I need, but is tedious to write out for larger arrays (say, 1:10 instead of 1:3):
g = @(x) f(x,1)+f(x,2)+f(x,3)
I tried something like
g = @(x) sum(arrayfun(@(y) f(x,y), 1:3))
but this does not work as soon as the size of x exceeds 1x1.
Thanks in advance.
f, so that it also accepts array input foryand not justx? Otherwise what are the allowed dimensions ofx?fsince it may be possible to vectorize the output in some manner.