MATLAB: Calling Matrix that is indexed, in a for loop

arrayscvxindexindexingMATLABmatricesmatrix arraymatrix manipulation

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.

Best Answer

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