How to change the number of variables based on the iteration number?


#1

Hello,

I am trying to run a cvx program where I the number of variables change with the iteration number (say, k). For example, when k = 2, the cvx routine has these variables (note: the variable n is fixed for different values of k):

cvx_begin
    variable q(n) complex;
    variable Q_11(n, n) hermitian;
    variable Q_12(n, n) hermitian;
    variable Q_21(n-1, n-1) hermitian;
    variable Q_22(n-1, n-1) hermitian;
    minimize ...

However, when k = 7, then the number of variables increase, and the routine looks like this:

cvx_begin
    variable q(n) complex;
    variable Q_11(n, n) hermitian;
    variable Q_12(n, n) hermitian;
    variable Q_13(n, n) hermitian;
    variable Q_14(n, n) hermitian;
    variable Q_15(n, n) hermitian;
    variable Q_16(n, n) hermitian;
    variable Q_17(n, n) hermitian;
    variable Q_21(n-1, n-1) hermitian;
    variable Q_22(n-1, n-1) hermitian;
    variable Q_23(n-1, n-1) hermitian;
    variable Q_24(n-1, n-1) hermitian;
    variable Q_25(n-1, n-1) hermitian;
    variable Q_26(n-1, n-1) hermitian;
    variable Q_27(n-1, n-1) hermitian;
    minimize ...

I would like to automate the variable declaration based on the value of k, without explicitly writing down all variable declarations every time I change k (as I have done in the code above). Is there a way to do this? Any help will be greatly appreciated. Thanks.

AJ


Is this possible for Assignment: Trace(M1_n*X) == Trace(M2_n*X)?
Variables and objective value zero:how to analyze the results from CVX (solver sdpt3)
Can I define a variable as a cell?
(Michael C. Grant) #2

Why not use multidimensional arrays? For instance:

variable Q_1(n,n,k) hermitian;
variable Q_2(n-1,n-1,k) hermitian;

Then you can access an individual ‘slice’ with colon notation; e.g., Q_1(:,:,k).

From the documentation:

Matrix-specific keywords can be
applied to n-dimensional arrays as
well: each 2-dimensional “slice” of
the array is given the stated
structure. So for instance, the
declaration

variable R(10,10,8) hermitian semidefinite

constructs 8 10×10
complex Hermitian PSD matrices, stored
in the 2-D slices of R.


#3

Thank you, mcg. Your suggestion worked like magic!

AJ