Create n symbolic variables
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
Hi, I would like to know if there's a way to create n differente symbolic variables (n is an input). Thank's in advanced
採用された回答
madhan ravi
2018 年 10 月 25 日
編集済み: madhan ravi
2018 年 10 月 25 日
EDITED
n=input('value of n?')
syms(sym('a',[1 n]))
a=(sym('a',[1 n])) %storing symbolic variables in a vector for convenience and also to use indexing
command window displays:
value of n?10
n =
10
a =
[ a1, a2, a3, a4, a5, a6, a7, a8, a9, a10]
>> a(1:6)
ans =
[ a1, a2, a3, a4, a5, a6]
>>
The above is strictly not recommended though please see http://www.mathworks.com/matlabcentral/answers/304528-tutorial-why-variables-should-not-be-named-dynamically-eval
9 件のコメント
madhan ravi
2018 年 10 月 25 日
The above shows an example of creating n symbolic variables
madhan ravi
2018 年 10 月 25 日
編集済み: madhan ravi
2018 年 10 月 25 日
If it’s what you want accept the answer so other people know the question is solved else let know
Walter Roberson
2018 年 10 月 25 日
Note that this creates a single variable, A, that contains a vector of symbolic expressions that refer to symbolic variables that live in the symbolic engine, same as if you had done
A = [sym('A1'), sym('A2'), sym('A3'), ... sym('A20')];
The code does not create variables A1, A2, A3, ... A20 at the MATLAB level. You will not see them in the workspace; you will only see A in the workspace.
If you need to create them at the MATLAB level, then chances are that you should be coding differently.
madhan ravi
2018 年 10 月 25 日
Yes sir I thought the OP asked to simply create variable to be used further in mathematical equations
madhan ravi
2018 年 10 月 25 日
Edited the answer sir Walter, thank you for pointing it out
Walter Roberson
2018 年 10 月 25 日
syms(sym('a',[1 n]))
That does work to create the variables in the MATLAB workspace, but any time you are creating a variable number of variables in the MATLAB workspace you should stop yourself and ask if it is strictly necessary to do so, as there is almost always a better way.
madhan ravi
2018 年 10 月 25 日
Oh yes sir Walter didn’t think of it actually thank you once again
one plus one is two
2018 年 11 月 20 日
Sorry for the late, thank you so much for your help!
madhan ravi
2018 年 11 月 20 日
Anytime :)
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で MATLAB についてさらに検索
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
