Calling Matrix that is indexed, in a for loop
조회 수: 3 (최근 30일)
이전 댓글 표시
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
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.
채택된 답변
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!