Calling Matrix that is indexed, in a for loop

2 vues (au cours des 30 derniers jours)
Hemant Tiwari
Hemant Tiwari le 12 Oct 2019
Commenté : Hemant Tiwari le 15 Oct 2019
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 commentaire
Stephen23
Stephen23 le 12 Oct 2019
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.

Connectez-vous pour commenter.

Réponse acceptée

Stephen23
Stephen23 le 12 Oct 2019
Modifié(e) : Stephen23 le 12 Oct 2019
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 commentaire
Hemant Tiwari
Hemant Tiwari le 15 Oct 2019
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!!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Produits


Version

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by