我怎么能动态地添加优化变量在一个循环中对我问题?

2视图(30天)
嘿:)
我定义了一个优化问题如下:
概率= optimproblem;
现在我想在一个循环中添加优化变量,如
因为我= 1:10
x = optimvar('测试',下界,0);
结束
第一次迭代中,变量的名称应该x_1、第二迭代应该x_2等等。
我试着这样的东西,但它不工作:
(“间”int2str (i)] = optimvar (ans,下界,0);
我的下一个方法是定义一个结构体变量如下:
因为我= 1:10
ans = mat2str (this_is_a_test(我,:));
ans = regexprep (ans, \ [(. *) \] ', ' $ 1 ');
ans = strrep (ans”、“,”);
ans = strcat (Test_, ans);
var =答;
变量(.var) = optimvar (ans,下界,0);
结束
在获得优化变量的结构,我不能到prob.Variables“转移”
我将非常高兴如果有人能帮我做这个:)
如果你需要更多的信息,不要犹豫地问。
最好的问候,
迈克
8的评论
Stephen23”class=
Stephen23 2023年3月22日
“这是一个n * m双。它的内容是只有1和2组成的。”
this_is_a_test =兰迪(1:2 10 3)
this_is_a_test = 10×3
2 1 1 1 2 2 2 1 1 1 2 1 2 2 2 1 1 1 2 1 2 2 1 1 1 2 1 1 2 2
var_names = [];
i = 1:10
ans = mat2str (this_is_a_test(我,:));
ans = regexprep (ans,“\ [(. *)\]”,“1美元”);
ans = strrep (ans,' ',);
ans = strcat (“Test_”,ans);
var_names = [var_names;ans);
结束
V0 =字符串(var_names)
V0 =10×1的字符串数组
“Test_122”“Test_211 Test_211”“Test_121”“Test_222”“Test_111”“Test_212”“Test_211”“Test_121”“Test_122”
这是一个简单的方法来生成这些名字:
V1 =“Test_”+加入(字符串(this_is_a_test),”“)
V1 =10×1的字符串数组
“Test_122”“Test_211 Test_211”“Test_121”“Test_222”“Test_111”“Test_212”“Test_211”“Test_121”“Test_122”

登录置评。

答案(0)

社区寻宝

找到宝藏在MATLAB中央,发现社区如何帮助你!

开始狩猎!