2

I have this script:

function [ G ] = evalF( F,x,n )

    G=zeros(n,1);
    xcell = num2cell(x);

    for i=1:n
        f = F(i)
        a = f(xcell{:})
    end

end

and these two variables defined:

F = 

    @(x1,x2)6+2*x1^1+3*x2^2    @(x1,x2)3+3*x1^1+2*x2^2

x =

     1     2

and when I run it like this:

evalF(F,x,2)

but I get the error:

Index exceeds matrix dimensions.

Error in evalF (line 8)
        a = f(x{:})

I don't see what's wrong... Can anyone help?

I turn an array into arguments, more info about that is here How to split an array as argument values in matlab?

1 Answer 1

2

This is because it is trying to index f instead of run the function stored in f. Why? f is a cell containing a function handle rather than a function handle.

To make f a function handle:

f = F{i};
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.