How can i assign variable in program.

1 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 Solver 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