How to define variable (for later use) that contains another defined variables

13 ビュー (過去 30 日間)
Marcin Fisior
Marcin Fisior 2022 年 5 月 19 日
コメント済み: Marcin Fisior 2022 年 5 月 19 日
Simple question
I.e I have variable N=Np/Ns. I;ve defined values for Np and Ns (which I may want to change later) but for my latter euqations I want to put simply only N, insteed of typing Np/Ns. How to define N in that way when I pun N in some equation it will pass Np/Ns.
I've tried simply N = 'Np/Ns' , but when I checked it by typing F = N + 1, insteed of simply calculate F = Np/Ns + 1, the output is strange 8 element matix.
  1 件のコメント
Stephen23
Stephen23 2022 年 5 月 19 日
"the output is strange 8 element matix"
Not very strange: you created a character vector and then added one to every character code:
1+'Np/Ns'
ans = 1×5
79 113 48 79 116
If you want to perform numeric/symbolic operations, character vectors are unlikely to be the solution.

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

回答 (1 件)

David Hill
David Hill 2022 年 5 月 19 日
np=5;ns=3;
n=@(np,ns)np/ns;
f=n(np,ns)+1;
np=8;ns=5;
f=n(np,ns)^2+3;
  6 件のコメント
Torsten
Torsten 2022 年 5 月 19 日
編集済み: Torsten 2022 年 5 月 19 日
As Stephen says, put all your computations - one after the other - in a function. Then you don't need to combine function handles, but you can simply work with variables.
Marcin Fisior
Marcin Fisior 2022 年 5 月 19 日
Yeah, now I see that will be best approach. Thanks

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

カテゴリ

Help Center および File ExchangeNumeric Types についてさらに検索

タグ

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by