当输入数量变化并使用符号时,如何使用函数句柄

时间:2012-08-24 05:10:28

标签: matlab

非常感谢您提供任何帮助......

我正在使用Matlab编写一般情况场景,因此函数的输入数量会有所不同。输入采用输入矩阵中元素数量的形式。

(我在下面提供了一个简单的玩具示例,以使我的问题更加清晰):

%# The code generates as many symbolic variables as necessary...

P1 = sym('P1')
P2 = sym('P2')
.
.
.
PN = sym('Pn')

%# I create a symbolic function of all the variables...

this  = (P1^2+P2^3+...+Pn^2)

%# I convert the symbolic function into a function...

that = matlabFunction(this)

%# Now I want to provide values for use in the calculation 
%# (I have a list of starting values for each P1...Pn)

other = that(???) --> 

%# ***Want to provide list of inputs that has as many values as the number 
%# of symbolic values I have created (which varies according to each case)****

我已经查看了其他问题,但也许我没有使用正确的搜索字词。当我使用句柄that时,是否有一种优雅的方式来提供可变数量的输入?

非常感谢你的帮助。 我是菜鸟,很欣赏它!

1 个答案:

答案 0 :(得分:1)

使用cell array

,而不是使用按递增顺序命名的变量
P = { sym('P1'), sym('P2'), ..., sym('Pn') }

然后你可以使用单元格数组解包来传递单元格数组中的项目作为参数列表:

that = matlabFunction(P{:})

请注意P(:)P{:}之间的区别(请参阅the documentation)。

相关问题