Solution for getting only one variable equal to 1 while other two variables 0.

I would like to give input variables as follows: For optimization purpose only one variable is to be given value equal to 1 while other two variables must be given 0. For example, if x(1)=1 then x(2) and x(3) must be equal to 0. Similarly, if x(2)=1, then x(1)=x(3)=0. No two variables should be equal to 1 at a time. Can anybody suggest me 'for loop' or 'if-else loop' for this problem. Thanks in anticipation.
function y=myfitness(x)
y= x(1)+x(2)+x(3);
end

 Respuesta aceptada

James Tursa
James Tursa el 13 de Abr. de 2018
Editada: James Tursa el 13 de Abr. de 2018
E.g., if I understand your request:
n = 3; % number of elements to use
for k=1:n
x = zeros(1,n);
x(k) = 1;
% You put code here to use x
end

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Preguntada:

el 13 de Abr. de 2018

Comentada:

el 17 de Abr. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by