Can a Comma Separated List be Generated Directly from a sym Array?

Can a comma separated list be generated directly from a sym array for use as input to a sym function?
>> xvar=sym('x',[1 4])
xvar =
[ x1, x2, x3, x4]
>> yvar=sym('y',[1 4])
yvar =
[ y1, y2, y3, y4]
>> f(xvar)=sum(xvar)
f(x1, x2, x3, x4) =
x1 + x2 + x3 + x4
>> f(yvar)
Error using symfun/subsref (line 177)
Symbolic function expected 4 input arguments but received 1.
>> temp=num2cell(yvar);
>> f(temp{:})
ans =
y1 + y2 + y3 + y4
Baically, is there a way evaluate f at the elements of yvar without having to create the temp variable, as needed for generailized code where the lengths of xvar and yvar are not known ahead of time?

 採用された回答

Walter Roberson
Walter Roberson 2021 年 2 月 1 日
編集済み: Walter Roberson 2021 年 2 月 1 日
Can a comma separated list be generated directly from a sym array for use as input to a sym function?
NO. But you can do it without an explicit temporary variable.
xvar=sym('x',[1 4]);
yvar=sym('y',[1 4]);
f(xvar) = sum(xvar);
f(struct('v', num2cell(yvar)).v)
ans = 

1 件のコメント

Paul
Paul 2021 年 2 月 1 日
Thank you. I never would have thought of that. Too bad that we can't do
f(yvar{:})
or
f(num2cell(yavr){:})

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeSymbolic Math Toolbox についてさらに検索

製品

リリース

R2019a

質問済み:

2021 年 1 月 31 日

コメント済み:

2021 年 2 月 1 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by