About"blkdiag". Dimensions with subscripts do not match

%It seems that there is a dimensional mismatch caused by type conversion, and if the keyword 
%"complex" is deleted, no error is reported, but this keyword can't be deleted.
%cvx 2.1
%How to solve it?
N = 10;
Kr = 3;
Q = zeros(N,N,N);
for j = 1:N
     Q(j,j,j) = 1;
variable W1(N-2,N-2,Kr) complex; 
variable W2(2,2,Kr) complex;
expression W(N,N,Kr) 
for i = 1:Kr
     if i==1
           W(:,:,i) = blkdiag(zeros(N-2),W2(:,:,i));
           W(:,:,i) = blkdiag(W1(:,:,i),zeros(2)); %error:Assignment dimensions with subscripts do not math  % cvx/subsasgn (line 79)  bx( :, ndx_x ) = by;   

minimize real(trace(sum(W,3))) 
subject to
for l = 1:N
      real(trace(Q(:,:,l)*sum(W,3))) <= 20;

This seems to be the same bug with blkdiag as occurred in Where is the mistake of this Convex optimization problem? .

My workaround there, as here, is to form the required expression by concatenation rather than using blkdiag .

   W(:,:,i) = [W1(:,:,i) zeros(N-2,2);zeros(2,N-2) zeros(2)];

Note: of course, the last block row can be combined into zeros(2,N), but I wrote it as I did for readability.

This executed without error for me, although unbounded without adding what are presumably your additional unprovided constraints.

1 Like

Yes,Thank you, you are right. thank you very much.