"error using reshape "

hi,I confront a problem of error using reshape. And I have no clue about what mistake i made.

cvx_begin quiet
variable Fi(2,M) complex
variable FI(M,M) hermitian
variable relax_scaler_D1(M,M,K) hermitian
variable relax_scaler_D2(M,M,K) hermitian

expressions    LMI(M+M+1,M+M+1,K)
k=1:1:K;
u=K:-1:1;
 for i=1:length(k)
    if k(i)==1||u(i)==1 
    LMI(M+M+1,M+M+1,K);
     LMI(k(i),:,:)=[relax_scaler_D1(:,:,k(i)), FI(k(i),:,:), Fi(k(i),:)';...
                FI(k(i),:,:)', relax_scaler_D2(:,:,k(i)) ,Fi(k(i),:)';...
                Fi(k(i),:), Fi(k(i),: ) ,1 ];
   end
end 
cvx_end

here is the error row. It’s dimension all matched. I don’t know where the reshape is functioning.
could anyone please help me out.

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

the error reported by matlab. what am i wrong?

All sorts of dubious things seem to be going on in
LMI(k(i),:,:)=[relax_scaler_D1(:,:,k(i)), FI(k(i),:,:), Fi(k(i),:)';... FI(k(i),:,:)', relax_scaler_D2(:,:,k(i)) ,Fi(k(i),:)';... Fi(k(i),:), Fi(k(i),: ) ,1 ];

Among other things,FI(k(i),:,:) has 3 dimensions, whereas it is declared with 2 dimensions - perhaps that is causing the error. As for LMI(k(i),:,:), do you mean to have k(i) in the last index position rather than the first - maybe that doesn’t cause an error message, but might not be what you want? I’ll let you diagnose and fix the rest of this.

You should first get this correct in “regular” MATLAB. For instance by setting the variables equal to ones of the appropriate dimensions instead of as CVX variables. Get MATLAB to accept your formulation without warning or error. When that is correct, change to CVX variable declarations.

I’m grateful for help me out.
here’s the result according to your guidance.

k=1:1:K;
u=K:-1:1;
M=20;
for i=1:length(k)
if k(i)==1||u(i)==1
LMI(k(i),:,:)=[ones(M,M), ones(M,M), ones(1,M)’;…
ones(M,M)’, ones(M,M) ,ones(1,M)’;…
ones(1,M), ones(1,M) ,1 ]
a=rand(k(i),M)
end
end

image
It turns out LMI is a 3 dimensions matrix which elments are 1. But after I add another dimension (iteration dimension k(i)), it doesn’t match again. How can I make this matrix iterate by k?

I don;t know what you are trying to do,.

expression LMI(M,M,K)
makes LMI have 3 dimensions., which can be interpreted as K matrices which are each M by M. The kth of these matrices is accessed by LMI(:,:,k)

This matrix is one of constrains of the problem. so I’m trying to get the iteration value for solving the problem. Maybe it’s dimension match error. I’ll try to fix it by myself and see if it could work.thank you !

I’ve tried a couple of times. This is how I code in regular matlab. Same matrix and same dimension of variables as before. And it gives me the result i want.

K=2;
k=1:1:K;
u=K:-1:1;

M=20;

for i=1:length(k)
if k(i)==1||u(i)==1%该循环体只适用于两个用户的
a1(:,:,k(i))=rand(M,M) + sqrt(-1)*rand(M,M)

 a2(:,:)=rand(M,K) + sqrt(-1)*rand(M,K)

 a4=a2(:,2)
 a3=a2'

LMI(:,:,k(i))=[a1(:,:,k(i)), a1(:,:,k(i)), a2(:,k(i));...
                a1(:,:,k(i)), a1(:,:,k(i)) ,a2(:,k(i));...
                a3(k(i),:), a3(k(i),:) ,1 ]

end
end

image

But strange thing is when i substitute these variables into those defined in cvx toolbox.

variable Fi(K,M) complex
variable FI(M,M) hermitian
variable relax_scaler_D1(M,M) hermitian semidefinite
variable relax_scaler_D2(M,M) hermitian semidefinite

LMI(:,:,k(i))=[relax_scaler_D1(:,:,k(i)), FI(:,:,k(i)), Fi(k(i),:)';...
                FI(:,:,k(i))', relax_scaler_D2(:,:,k(i)) ,Fi(k(i),:)';...
               Fi(k(i),:), Fi(k(i),:) ,1 ];

turns out :index exceeds matrix dimensions error
more strange thing is that it seems transpass this matrix and execute the code below the row.
why could that happen? A bug?

I really appreciate you can guide me.

“Regular” MATLAB is adding a 3rd dimension to a1 when you have a1(:,:,k(i))=rand(M,M) + sqrt(-1)*rand(M,M)

CVX does not expand variables. or expressions They have whatever dimensions they are declared with. So you should have

expression LMI(M,M,K)
variable relax_scaler_D1(M,M,K) hermitian semidefinite
variable relax_scaler_D2(M,M,K) hermitian semidefinite

etc. and make any similar changes wherever else they are needed.

You can apply transpose, reshape or permute to CVX variables or expressions; but if so, that needs to be done by explicit use of these. Otherwise, CVX variables and expressions have the dimensions they were declared with.

Finally It works. Thank you so much!