How can i assign variable in program.

1 回表示 (過去 30 日間)
Triveni
Triveni 2015 年 12 月 15 日
編集済み: Stephen23 2019 年 11 月 3 日
a = [a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15 a16 a17 a18 a19 a20];
assignin('base','a1',x(1));
assignin('base','a2',x(2));
assignin('base','a3',x(3));
assignin('base','a4',x(4));
assignin('base','a5',x(5));
assignin('base','a6',x(6));
assignin('base','a7',x(7));
assignin('base','a8',x(8));
assignin('base','a9',x(9));
assignin('base','a10',x(10));
assignin('base','a11',x(11));
assignin('base','a12',x(12));
assignin('base','a13',x(13));
assignin('base','a14',x(14));
assignin('base','a15',x(15));
assignin('base','a16',x(16));
assignin('base','a17',x(17));
assignin('base','a18',x(18));
assignin('base','a19',x(19));
assignin('base','a20',x(20));
x
  1 件のコメント
Stephen23
Stephen23 2015 年 12 月 15 日
編集済み: Stephen23 2019 年 11 月 3 日
In most cases assignin should be avoided for more effiicent ways of passing data between workspaces. The documentation states " Best Practice: Passing Arguments The most secure way to extend the scope of a function variable is to use function input and output arguments, which allow you to pass values of variables."
Numbered variables are a sign that you are doing something wrong.

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

採用された回答

Walter Roberson
Walter Roberson 2015 年 12 月 15 日
You use x but you do not define it.
  2 件のコメント
Triveni
Triveni 2015 年 12 月 16 日
I want random variable during optimization using fminunc. Can you help me how can i generate random variable during optimization??
Walter Roberson
Walter Roberson 2015 年 12 月 16 日
Using a random variable would result in a discontinuity, which fminunc() is not designed to handle.
Your question is not clear as to whether you want to use a random variable, or a value randomly selected from a list of potential values. If you want to select randomly from the variable named x then
random_x = x(randi(numel(x));

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSolver Outputs and Iterative Display についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by