Cvx in a loop

I am using the following code:

for i=1:1
    cvx_begin
        cvx_quiet(true)
        variables x(N,1)
        minimize( a(:,i)*x ) 
        subject to
            for j=1:N
                [eye(1) x(j);x(j) 1] == semidefinite(2);
            end
    cvx_end
    b(:,i)=x;
end

In each iteration, everything is the same except the value of the vector a. I want to repeat this loop for 1,000,000 iterations and it taks so long. Is there anyway I can save time in modeling time in cvx and just change the vector a?

Generally speaking, no, there is no way to save the modeling time in CVX. CVX must reinterpret the entire model every time. But for most models, the modeling time should be less than the solver time, so the cost should not be significant. If the modeling time is significantly longer than the solver time, then that is either a bug in CVX or a problem with the way it is modeled in the first place.

In this case, I’d say your problem is the latter. It’s never a good idea to use for loops in Matlab whenever they can be rewritten using vector operations. This is even more important within CVX models. So you should be endeavoring to eliminate the for loop. But why are you using these 2x2 semidefinite constraints in the first place? After all, for scalar x,
$$\begin{bmatrix} 1 & x \ x & 1 \end{bmatrix} \succeq 0 \quad\Longleftrightarrow\quad 1 - x^2 \geq 0 \quad\Longleftrightarrow\quad |x| \leq 1$$
So why can’t you simply rewrite the inner for j=1:N loop as abs(x) <= 1 or -1 <= x <= 1 or norm(x,Inf) <= 1? Not only does this eliminate the for loop, but it eliminates the nonlinearities as well.

In light of mcg’s response, your problem has a closed form solution:

$$\min {a_i^Tx:|| x||_\infty\le 1}=-||a_i||_1.$$

In other words, x=-\text{sign}(a_i) and so b_{ji}=-\text{sign}(a_{ji}).

Absolutely. I’m embarrassed I did not see this :stuck_out_tongue: