Borrar filtros
Borrar filtros

Need help to create a matrix for the use of intlinprog!

3 visualizaciones (últimos 30 días)
Taner Cokyasar
Taner Cokyasar el 10 de Jul. de 2016
Comentada: Taner Cokyasar el 11 de Jul. de 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.

Respuesta aceptada

Stephen23
Stephen23 el 11 de Jul. de 2016
Editada: Stephen23 el 11 de Jul. de 2016
Just use sub2ind:
>> Z = [0;0;1;0;1;0;0;1;0]
Z =
0
0
1
0
1
0
0
1
0
>> Y = zeros(3,9);
>> Y(sub2ind(size(Y),1:nnz(Z),find(Z).')) = 1
Y =
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
  1 comentario
Taner Cokyasar
Taner Cokyasar el 11 de Jul. de 2016
This was exactly what I was looking for. Thank you so much Stephen.
dpb, you typed the same thing as well. I could have understood yet. Thanks for all your answers.

Iniciar sesión para comentar.

Más respuestas (1)

KSSV
KSSV el 11 de Jul. de 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 comentarios
dpb
dpb el 11 de Jul. de 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).
Taner Cokyasar
Taner Cokyasar el 11 de Jul. de 2016
That is true dpb. However, I couldn't make your codes work :(. Your code is really confusing to me. For my first question given in that link, you used 'm' for the whole matrix. I changed that 'm' and it worked. However, for my second question, since I couldn't properly understand the way you are doing it, I couldn't fit your codes into mine. I am pretty sure that you understood the problem and able to provide the correct formulation, however I can't understand it. I simplified the problem in the following link:
Maybe, you can take a look at there. I would really appreciate. Thanks.

Iniciar sesión para comentar.

Community Treasure Hunt

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

Start Hunting!

Translated by