evaluate dynamic variables in a loop and assign each one with optimvar.

Hi;
i created a number of variables like the following:
for j=1:N
eval(['b' num2str(j) '= j']);
end
i need to assign each b1,b2,b3,.... as the following inside the loop.
b1=optimvar('b1','Type','integer','LowerBound',0,'UpperBound',1);
b2=optimvar('b2','Type','integer','LowerBound',0,'UpperBound',1);
.
.
.
please help.

댓글 수: 4

if there is different way no problem with it.
Cell array, or dynamic field names on a struct array.
give me a hint how to start writing the code.

댓글을 달려면 로그인하십시오.

 채택된 답변

infinity
infinity 2019년 6월 26일
Hello,
This small example may help you
clear
N = 3;
b = cell(1,N);
for i = 1:N
a = sprintf('b%d',i);
b{i} = optimvar(a,'Type','integer','LowerBound',0,'UpperBound',1);
end

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2019년 6월 26일

댓글:

2019년 6월 26일

Community Treasure Hunt

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

Start Hunting!

Translated by