如何计算 MATLAB 方程中符号变量的个数?
How to count the number of symbolic variables in an equation in MATLAB?
假设我有三个等式,如 [x1+x2+x3, -x1, x1+x2+1]
。
MATLAB 中是否有任何函数可以用来计算每个等式的符号变量的数量?
提前致谢。
如果你想知道总个变量,你可以使用symvar
如下:
>> syms x1 x2 x3 % define symbolic variables
>> y = [x1+x2+x3, -x1, x1+x2+1] % define symbolic equation
>> numel(symvar(y)) % get number of sumbolic variables
ans =
3
要获取每个方程的变量个数,可以使用下面的方法,如@SardarUsama所建议:
>> arrayfun(@(t) numel(symvar(t)), y)
ans =
3 1 2
这将遍历方程并获取每个方程的变量数。
假设我有三个等式,如 [x1+x2+x3, -x1, x1+x2+1]
。
MATLAB 中是否有任何函数可以用来计算每个等式的符号变量的数量?
提前致谢。
如果你想知道总个变量,你可以使用symvar
如下:
>> syms x1 x2 x3 % define symbolic variables
>> y = [x1+x2+x3, -x1, x1+x2+1] % define symbolic equation
>> numel(symvar(y)) % get number of sumbolic variables
ans =
3
要获取每个方程的变量个数,可以使用下面的方法,如@SardarUsama所建议:
>> arrayfun(@(t) numel(symvar(t)), y)
ans =
3 1 2
这将遍历方程并获取每个方程的变量数。