Reputation: 125
Matlab has the function Legendre that returns to an integer m and real number r a vector of length m+1.
Now I want to define another function fun that gives me only the first component of this vector per fixed m, so I want to define a function fun(m,r) that gives me the first component of the vector legendre(m,x). The point is that fun(m,r) should also be a function, just like legendre. Does anybody know how to do this?
Upvotes: 0
Views: 127
Reputation: 112659
Define the function as follows:
function out = fun(n,x)
temp = legendre(n,x); %// store output of "legendre" in a temporary variable
out = temp(1); %// return only desired element
Of course, this should be placed in a file fun.m
within Matlab's path.
Alternatively, if you are feeling hackish, you can use
getfield(legendre(n,x), {1})
to extract the first element of legendre(n,x)
directly (without a temporary variable). This allows defining fun
as an anonymous function as follows:
fun = @(n,x) getfield(legendre(n,x), {1});
Upvotes: 3