How to index primal variables?


I have a problem formulation issue with my simulation.
Depending on my simulation parameters, I need to declare a set of complex vector variables with different dimensions (the dimension also depends on other simulation parameters). So I want to write something like this:

n = [3, 4, 2, 3, 3]; 
for i = 1:5
    % the i-th item in cell x is a vector of dimension n(i)
    variable x{i}(n(i)) complex  

Obviously, this will not compile with CVX because the x{i} is reserved for dual variables. I cannot also use a 3D array since the dimensions of the vector variables can be different and I want to avoid rewriting the formulation when I change my simulation parameters.
Perhaps, a shortest version of my question: is it possible to index primal variables in CVX? If not, can you please suggest a workaround? Thanks.

I would do this:

m = length(n);
variable xx(max(n),m) complex;
x = cell(1,n);
for i = 1 : m
    x{i} = xx(1:n(i),k)

Don’t worry about all those extra variables you are creating, the presolver will eliminate them. But if you’re determined, you could create do variable xx(sum(n)) complex and split it apart with some clever indexing math.

You will find that when CVX is complete, the cell array x will indeed be populated with the numerical values of its variables.

Thank you so much Michael! It entirely solved my problem. :slight_smile: