Amir Sagiv
Amir Sagiv

Reputation: 386

Creating anonymos function in Matlab with one array type argument

I'm working with the function handler syntax in Matlab:

func = @(x,y) x+y

But when I try to do the following I fail. I want to pass to another function (specifically I'm implementing some version of Newton Raphson, but that's besides the point) for which I'm passing both a point in $R^d$ and a real function on that domain. I leave it to the invoker of the method to check that dimensions agree, and therefore I'm unaware of d. I'd like to call the function with one d-dimensional argument, represented as an array. but this syntax doesn't seems to be supported. Any help?

What I'd like to do:

    x= [1,2]
    y = func(x)

Upvotes: 1

Views: 35

Answers (1)

NKN
NKN

Reputation: 6434

if you have just two input variables this is the solution:

>> func = @(x,y) x+y
>> func(1,2)

ans =

    3

but if you have more than two variables and you want to work with vectors do this:

>> func2 = @(x) sum(x)

>> x=[1,2,3];
>> func2(x)

ans =

     6

Upvotes: 0

Related Questions