There's a trick you can use with num2cell
. What you would do is convert each parameter into its own individual cell, then use the :
to deal out the parameters. In other words, you would do this:
x = rand(2,1);
c = num2cell(x);
f(c{:})
Repeating your code above, and using what I have defined, this is what I get:
%// Your code
x=sym('x',[2 1]);
func=x.'*x;
f=matlabFunction(func);
x=rand(2,1);
%// My code
c = num2cell(x);
%// Display what x is
x
%// Display what the output is
out = f(c{:})
I am also displaying what x
is and what the final answer is. This is what I get:
x =
0.1270
0.9134
out =
0.8504
This is also the same as:
out = f(x(1), x(2))
out =
0.8504
In general, you can do this with any dimensional vector you want, provided that your function you're defining can handle that many inputs / dimensions.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…