MATLAB: How to automatically constrain some of the unknown values in an optimization problem

arrayautomatically constrainceqconstraintoptimizationOptimization Toolbox

I have an optimization problem with several unknown values (x) which is a part of an array:
A=[0 0 x(1) x(2) 0 0 0 x(3) x(4) x(5) 0 0 0 x(6) 0 0 x(7) x(8)];
The x-values are to be optimized but each x-'group', which is separated by the zeros, are to be equal to each other. ex.: x(1)=x(2) and x(3)=x(4)=x(5) and so on. x(6) is not to be equal to another x because it is not in a 'group' with other x's, therfore x(6) will not be constrained.
I am supposed to write this constraint as ceq(x)=[..] in a constraint function.
Is there any automatic coding that can constrain these x-values in the way i explained?

Best Answer

If you know in advance which elements of the x-vector are grouped together, you can simply use Aeq and beq to define the equality constraints. No need to use the nonlinear constraints option in ceq.