Need help to create a matrix for the use of intlinprog!
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Taner Cokyasar
le 10 Juil 2016
Commenté : Taner Cokyasar
le 11 Juil 2016
Hello,
I am a beginner. I have the following sample vector named Zvalues:
Variable Name Zvalues
Z11 0
Z12 0
Z13 1
Z21 0
Z22 1
Z23 0
Z31 0
Z32 1
Z33 0
I have created a zero 3x9 sized matrix as following:
Y= zeros(3, 9);
I want to fill this matrix with an if condition, as the following example:
if Zvalues(3,1) == 1;
Y(1,3) = 1;
if Zvalues(5,1) == 1;
Y(2,5) = 1;
if Zvalues(8,1) == 1;
Y(3,8) = 1;
The following matrix is what I want to reach at the end. 'Y's represent variable names, they are not a part of the matrix .
Y11 Y12 Y13 Y21 Y22 Y23 Y31 Y32 Y33
0 0 1 0 0 0 0 0 0
0 0 0 0 1 0 0 0 0
0 0 0 0 0 0 0 1 0
I thought that I might use for loop and if commands, however, I couldn't really successfully create a code for it. This is what I could have think of:
for k=1:i*m
if Zvalues(k,1) == 1
Y(1,k) = 1
end
end
where, i*m is 9. The formulation is absolutely wrong. Please help me to fix it. Thanks.
The question is edited to make it more clear. If you see anything unclear, please let me know.
0 commentaires
Réponse acceptée
Plus de réponses (1)
KSSV
le 11 Juil 2016
clc; clear all ;
z = [0
0
1
0
1
0
0
1
0];
sigmas = zeros(3, 9);
Ys = zeros(3, 9);
if z(3,1) == 1;
sigma(1,3) = 1;
Ys(1,3) = 1;
end
if z(5,1) == 1;
sigma(2,5) = 1;
Ys(2,5) = 1;
end
if z(8,1) ==1;
sigma(3,8) = 1;
Ys(3,8) = 1;
end
Aeq = [sigma, Ys]
3 commentaires
dpb
le 11 Juil 2016
Isn't this the same question we've been discussing at <help-for-creating-constraint-with-if-conditions>? The solution I gave there is general (that's why I went the route I did to produce one that is independent of size simply given the input array).
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!