Converting CVX object to double


I have gone through CVX documentation and I know that it is not possible to convert cvx objects to double, at least using the MATLAB double command.

But I want to extract a numerical value from a cvx object, because I want to use it for array indexing. Is there any other way about it?


CVX variables can’t be used in an array index.After CVX completes 9f successfully), all CVX variables are populated by their optimal numerical values and may be used the same as any other MATLAB variables.

If you show more clearly what you are trying to accomplish, perhaps a more specific assessment can be made as to whether and how it can be done. It may be that use of binary or integer variable declarations in CVX allow you to indirectly accomplish what you want with indexing, but not necessarily in a convenient manner.

Hello Mark,
Thank you for the quick response. I am trying to solve an optimal control problem in which the constraints are a function of the state variables, for which I need to define an index ‘j’. Index ‘j’ represents x(1) in the upper bound constraint x_max(j,i). ‘i’ is the time index.
See below code and picture:
The upper bound x_max on variable x(2) is function of x(1).

n = min(size(x_max)); dt=0.1; m=35000; dx=5;
variable u(n)
variable x(2,n)
expressions xveh j
minimize sum(max(0,u))
subject to
x(1,1) == 0;
x(2,1) == 0;
x(1,n) == 200;
x(2,n) ==0;
-40000 <= u(1:n) <= 35000;
for i =1:n-1
x(:,i+1) == [1 dt;0 1]*x(:,i) + [0;dt/m]*u(i);
xveh = x(1,i+1);
j = xveh/dx + 1;
0<= x(2,i+1) <= x_max(j,i+1);

I think you are going to have to introduce binary or integer variables, used in something along the lines of Big M, in order to specify which constraint (upper bound) should be chosen for enforcement (an If Else.kind of thing). It may be messy and I don’t have the energy to work out the details, so best of luck if you try to do this.