CVX error: "To RESHAPE the number of elements must not change."


Consider the following simple SDP program.


cvx_begin SDP

    variable X1(n,n) hermitian semidefinite
    variable X2(n,n) hermitian semidefinite

   minimize( trace(X1) + trace(X2) )
    subject to

   k = trace(X1) + trace(X2);
   blkdiag( k*eye(n) + X1, X2 ) >= mat


This causes the following error:

Error using reshape
To RESHAPE the number of elements must not change.

Error in cvx_reshape (line 52)
        x = reshape( x, s );

Error in cvx/cvx_readlevel (line 7)
y = cvx_reshape( y, x.size_ );

Error in cvxprob/newcnstr (line 94)
tx = cvx_readlevel( x );

Error in  >=  (line 21)
b = newcnstr( evalin( 'caller', 'cvx_problem', '[]' ), x, y, '>=' );

Error in cvxtest (line 12)

Rewriting blkdiag(k*eye(n)+X1,X2)>=mat as mat-blkdiag(k*eye(n)+X1,X2)<=0 changes the error to:

Matrix dimensions must agree.

Error in  +  (line 73)
    bad = vx + size( remap_plus, 1 ) * ( vy - 1 );

Error in  -  (line 21)
z = plus( x, y, true, cheat );

Error in cvxtest (line 14)

The matrix dimensions obviously agree, so I am not sure what can be wrong. I would appreciate any help because I keep running into this problem in my code.

I am using CVX 2.1 in Matlab 2016b.


Could someone please confirm whether this is a possible bug with CVX? One can easily rewrite the problem as

 subject to

X+blkdiag(k*eye(n),zeros(n,n))>= mat

which CVX accepts and solves, which makes me think that there is a problem with how CVX derives the dual of the problem in the original post.

(Guangxia Cheng) #3

Hi, there! Recently, I have come into the similar problem as you do and get the same error instructions.
After careful check and lots of experiments , I finally located the problem and solved it. It turns out that the cause of the problem is the wrongly use of the library function ‘sparse’ which is called in another library function ‘blkdiag’ .
To locate the function, firstly , you should pay much attention while checking the function ‘blkdiag’ as there maybe several functions given the same name. To save your time , I paste the path in my computer here: ‘E:\application\matlab凸优化工具箱\cvx\builtins@cvx\blkdiag’. Then check the library function ‘sparse’ which is called in ‘blkdiag’ and switch the position of last two parameters and check your problem again . In this way, your problem should be solved. The corrected function is showed below.

(Michael C. Grant) #4

This is great help! Thank you.