Reputation: 23
I have an anonymous function with 10 variables now I want to evaluate it with data in a p=1x10 matrix like this:
answer=func(p(1),p(2),p(3),p(4),p(5),p(6),p(7),p(8),p(9),p(10))
i don't want to use this, i need something like:
answer=func(p(:))
but it generates error can anyone give me a solution?
Upvotes: 2
Views: 67
Reputation: 25232
You seem to have some basic misunderstandings using anonymous functions and its syntax.
For what I think you want to do, you basically have three options:
Define the function with 10 input parameters and provide 10 input values - OR expand an input array as comma separated list using {:}
which requires an intermediate num2cell
step:
func1 = @(a,b,c,d,e,f,g,h,i,j) a + b + c + d + e + f + g + h + i + j
p = num2cell(p)
answer = func1(p{:})
Define the function with 1 input parameters using an array with 10 values and provide this array:
func2 = @(p) p(1) + p(2) + p(3) + p(4) + p(5) + p(6) + p(7) + p(8) + p(9) + p(10)
answer = func2(p)
The last option to use varargin
is really case dependent and could look like:
func3 = @(varargin) [varargin{:}]
p = num2cell(p)
answer = func3(p{:})
Upvotes: 4