# How can I declare a block diagonal variable in cvx matlab?

Hello,
my problem is the statement of a variable defined as a block matrix, because the variable matrix must be in the form:
X= \begin{vmatrix}
\mathbf{R1} & 0 & \mathbf{…} & 0 ; \
0 & \mathbf{R2} & \mathbf{…} & 0 ; \
\mathbf{…} & \mathbf{…} & \mathbf{…} & \mathbf{…} ; \
\mathbf{…} & \mathbf{…} & \mathbf{…} & \mathbf{Rn} \
\end{vmatrix}
where Ri must be a simmetry semidefinite matrix.
In fact the optimizate problem is:
min(norm(AXB-C))
subject to
X simmetry semidefinite matrix
Thanks you

I try this solution:Imposing a block diagonal matrix form and I set:

variable R1(2,2) symmetric
variable R2(2,2) symmetric
variable R3(2,2) symmetric
variable R4(2,2) symmetric
variable R5(2,2) symmetric
variable R6(2,2) symmetric
variable R7(2,2) symmetric
variable R8(2,2) symmetric
variable R9(2,2) symmetric
variable R10(2,2) symmetric
variable R11(2,2) symmetric
variable R12(2,2) symmetric
expression X
X = blkdiag(R1,R2,R3,R4,R5,R6,R7,R8,R9,R10,R11,R12);
X==semidefinite(24);
minimize(norm(A*X*B-C));


but the algoritm is very very slow.
Can I fix it?
Thank you

You cannot declare a block diagonal variable, but you could construct one.

For instance:

variable R1(n,n) semidefinite
variable R2(n,n) semidefinite
variable R3(n,n) semidefinite
R = blkdiag(R1,R2,R3);


That said, it might be better to leave the blocks separate and expand out the matrix multiplications in terms of the individual blocks.

CVX is built for convenience, not for speed. Per my suggestion above, your best bet is not to form the block diagonal matrix, but instead expand out the matrix multiplication.

Ah, actually, my solution above does have an important difference. Declaring 12 2x2 SDP blocks is definitely faster than declaring one 24x24 SDP block.

OP’s solution has the line X==semidefinite(24); which is redundant and unnecessary given the previous lines. Is that line of code slowing things down vs. not having it? Which is not to say that expanding out the matrix multiplication is not better.

The way he has declared it here, it’s not redundant. Note that in my solution, I used the keyword semidefinite, which includes the SDP constraint. In the formulation here, he used the symmetric keyword. So without the X==semidefinite(24) constraint, it would not be complete.

Sorry, I misread symmetric as semidefinite, and therefore thought X==semidefinite(24); was redundant.

Sorry, I do not know how to separate the problem.In the simple case that A is a matrix 3x2, where every element is 2x2,so this matrix is 6x4, and X is 2x2, where every element Ri is 2x2, so this matrix is 4x4,and B consists of 2 identity matrix, how can I fix it?

It’s just block multiplication. Partition A and B according to the structure of X; e.g., divide A into groups of 2 columns each, B into groups of 2 rows each. If it’s not clear how to do that, then I wouldn’t worry about it, just leave it as is.

The code is much faster but I would like to recommend another, thanks. I set cvx_precision best,I would like to know if there is a way to further lower the error.

No, the best you can do is try another solver.

thank you.