Calling Matrix that is indexed, in a for loop

조회 수: 3 (최근 30일)
Hemant Tiwari
Hemant Tiwari 2019년 10월 12일
댓글: Hemant Tiwari 2019년 10월 15일
Hello
I am fairly new to MATLAB and coding in general. Your patience and understanding is appreciated. This is part of a bigger jobshop scheduling problem that I am trying to model using CVX.
Say I have a matrix "T" of "Ns" number of elements.
I want to define "Ns" matrices from this master matrix "T". Which are manually defined and indexed as T[1], T[2]...T[A]. They all will have different sizes but same number of rows, which is equal to 1, as the master matrix "A".
example:
A = 5;
T = [1 2 3 4 5];
T[1] = [1 2];
T[2] = [2 3 4];
T[3] = [3 4 5];
T[4] = [3 4 5];
T[5] = [1 5];
I want to define them this way so I can use them in a "for" loop, which runs for a constraint and assumes the value of matrix elements (1, 2, 3, 4, or 5) based on the index it is at.
For example:
If the index is 1, the value that say "k" can take is 1 or 2. But not 3, 4, or 5. If the loop is at "A =2", "k" can take values 2,3, or 4. I hope this is making sense.
As I said this is a job shop scheduling model and following is the constraint I am trying to model (using cvx):
Nm is number of machines
Np is number of time periods
Ns in number of power states
Nj is number of jobs
W is another binary variable indexed over Nm, Ns, and Np.
W(i,s,p) <= sum(over k which belongs to T)W(i,k,p+1)
here is what I need as output:
for i=1, s=1,p=1,
W(1,1,1) <= W(1,1,2)+W(1,2,2)
for i =1, s=2,p=1,
W(1,2,1) <= W(1,1,2)+W(1,2,2)+W(1,3,2)
here is my current code:
for i = 1:Nm
for p = 1:Np-1
for s = 1:Ns
for k = 1:T (this is 100% wrong)
W(:,:,:) <= sum (W(:,k,p+1));
end
end
end
end
How can I acheive this? Any kind of input to help me look in the right direction is appreciated.
Thank you in advance.
  댓글 수: 1
Stephen23
Stephen23 2019년 10월 12일
Note that your indexing syntax it is definitely not MATLAB syntax, it looks like some other language. To learn basic MATLAB concepts, such as how to do indexing in MATLAB, you should work through the introductory tutorials:
Even if you have some experience of other languages, MATLAB has some important differences that make these tutorials worthwhile.

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

채택된 답변

Stephen23
Stephen23 2019년 10월 12일
편집: Stephen23 2019년 10월 12일
The easiest approach is to use a cell array:
T{1} = [1,2];
T{2} = [2,3,4];
T{3} = [3,4,5];
T{4} = [3,4,5];
T{5} = [1,5];
for k = 1:numel(T)
T{k} % do whatever you want with the k-th vector
end
  댓글 수: 1
Hemant Tiwari
Hemant Tiwari 2019년 10월 15일
Thank you very much for your input @Stephen Cobeldick!
I am using cvx (http://cvxr.com/) which provides modelling framework. I will definitely look into the tutorials thank you very much!!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by