79299232

Date: 2024-12-21 10:22:49
Score: 0.5
Natty:
Report link

I've been dealing with the same problem. In my case, I wanted to construct a nonlinear system of equations to be solved with fsolve from a cell array that contains each equation as one anonymous function in each cell, thus a for-loop approach for evaluations wouldn't work. I found this one-line-almost-illegible statement that works fine for this case:

F = {@(x,~)x, @(x,y)x+y, @(~,y)y^2}; %The cell array you asked as example

fun = @(x,y)cell2mat(arrayfun(@(xval,yval) cellfun(@(c) c(xval,yval),F),x,y,'UniformOutput',false))

Now fun will be an anonymous function that outputs the evaluation of F elements as a matrix. If you prefer the output as a cell array, remove 'cell2mat'.

Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Unregistered user (0.5):
  • Low reputation (1):
Posted by: XabierGC