Creating Dynamic Variables with Constraints (avoid for loop)


I am trying to avoid using for loops as instructed in the cvx user guide, however, I cant seem to figure out how to do so for the constraints:
My code:

% declare initial variables - avoid doing this in while loop ever time
% size of X is set at runtime and does not change
dim = 2;
Zcell = cell(length(X));
for i = 1:length(X)
    Zcell{i} = eval(sprintf('Z%d (%d,%d)',i,d,d));

while (abs(diff) >= tol)
    cvx_begin quiet
        variables Zcell
        minimize( myFunction(Z) )
        subject to

How can I substitute ???? with something that says each Zi is PSD?

If all of the matrices are the same size, put them in an N-D array. Then you can constrain them all to be positive definite at once:

variables Z(d,d,n) semidefinite


variables Z(d,d,n)
Z == semidefinite([d,d,n])

That said, if you can’t avoid a for loop, don’t worry about it. It’s not going to prevent the model from being solved. It’s a performance issue, but your first priority is to get the model to work!

thanks! It does indeed work, we’re in the process of trying to speed it up. Ill try that thanks!