フィルターのクリア

how to count the number of terms in syms

3 ビュー (過去 30 日間)
EllaD
EllaD 2020 年 10 月 20 日
編集済み: EllaD 2020 年 10 月 20 日
Hello!
I have a syms variable let's say A which is an addition of many terms say: A=a+b-c*a+a*b-d+... I want to count the number of added terms in A, how can I do that? my matlab version is 2012 by the way.
Thank you!

回答 (3 件)

Ameer Hamza
Ameer Hamza 2020 年 10 月 20 日
Try this
syms a b c d
A=a+b-c*a+a*b-d+a*c;
num_terms = numel(regexp(char(A), '[+-]?'))+1;
  1 件のコメント
EllaD
EllaD 2020 年 10 月 20 日
This worked like a charm! Thank you :)

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


Walter Roberson
Walter Roberson 2020 年 10 月 20 日
feval(symengine, 'nops', A)
In newer releases you can numel(children(A)) but that is several years after your version
  1 件のコメント
EllaD
EllaD 2020 年 10 月 20 日
編集済み: EllaD 2020 年 10 月 20 日
Thanks but it seems that this doesnt work, I have 87 terms but this outputs 3.
EDIT: It is strange though, I checked this code for 5 terms and it does work.

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


KSSV
KSSV 2020 年 10 月 20 日
You can get the number of sym variables present in the work spce using:
s = whos ;
iwant = nnz(strcmp({s(:).class},'sym'))

Community Treasure Hunt

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

Start Hunting!

Translated by