How to evaluate a cell of function handles?

Hi,
suppose that we have cell arrays of function handles Rnum, for example:
fnum=matlabFunction(y);
Fnum=@(x)integral(fnum,0,x);
Rnum{i,:}=@(x) exp(-(Fnum(x)).^beta_hat(i,:));
with i-number of different beta_hats. y is a function which consists of i-number of symbolic variables.
Now, I would like to evaluate all functions at the points: x = 0:100:4000.
Does anyone know how it works?
PS: If I do it for only one point x = 400, I can use the following code which works very well: cellfun(@(c) c(x),Rnum).

 채택된 답변

Kirby Fears
Kirby Fears 2016년 1월 20일

3 개 추천

Max,
If your function handles can accept an array of x values and return an array of results, then your current implementation works fine. It would look like this:
x = 0:100:4000;
resultsCell = cellfun(@(c) c(x),Rnum,'UniformOutput',false);
Otherwise you can use arrayfun or a loop to go over the different x values. A loop might actually be easier to read:
x = 0:100:4000;
resultCell = cell(numel(x),1);
for xval = 1:numel(x),
resultCell{xval} = cellfun(@(c) c(x(xval)),Rnum);
end
The arrayfun approach would look like this:
resultCell = ...
arrayfun(@(xval) cellfun(@(c) c(xval),Rnum),x,'UniformOutput',false);
Hope this helps.

댓글 수: 1

Max
Max 2016년 1월 21일
Hey Kirby, thnx for your solution. Now, it works very well. That´s exactly what I was searching for.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Rebecca Krosnick
Rebecca Krosnick 2016년 1월 20일

1 개 추천

Using the "cellfun" function as you have pointed out is a good way to apply a function to each cell in a cell array.
If you would like to call the function on multiple input points x, then you should either call "cellfun" for each x value, or change your implementation of your function c such that it will take as an argument the vector 0:100:4000 and produces the appropriate results.

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

질문:

Max
2016년 1월 18일

댓글:

Max
2016년 1월 21일

Community Treasure Hunt

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

Start Hunting!

Translated by