# How to solve 'Index beyond matrix dimension'?

hi,
I encountered problems when using cvx, and the code is as follows :

``````cvx_begin
variable   W_W(N,N)  semidefinite
variables   t0 t(K,1) p_p(K,1);
expressions R1(K,1) R2(K,1) R3(K,1) R4(K,1) R5(K,1)
for k=1:K
(delta^2)/p_p(k,1)/(g(k)^2))+rel_entr((t(k,1)^2)*(delta^2)/p_p(k,1)/(g(k)^2),t(k,1)+(t(k,1)^2)*(delta^2)/p_p(k,1)/(g(k)^2)))/log(2);
%R1(k,1)=t(k,1)*log(1+p_p(k,1)*g(k)^2/(t(k,1)*delta^2))/log(2);
R1(k,1)=-rel_entr(t(k,1),p_p(k,1)*g(k)^2/delta^2+t(k,1));

end
for k=1:K
R2(k,1)=p_p(k,1);
R3(k,1)=(t0+t(k,1))*PkC;
R4(k,1)=t(k,1)*PIR;
end
R5(1,1)=chi*(G1*W_W(N,N)*G1');
R5(2,1)=chi*(G2*W_W(N,N)*G2');
maximize sum(R1)-eta*(sum(R2)+t0*(PPS+N*Pe)+sum(R3)+sum(R4)+trace(W_W(N,N))-sum(R5));
subject to
t0+sum(t)<=T;
t0>=0;
trace(W_W(N,N))<=t0*Pmax;
for k=1:K
t(k,1)>=0;
p_p(k,1)+PkC*(t0+t(k,1))<=chi*(G1*W_W(N,N)*G1');
R1(k,1)>=Rkmin;
end
cvx_end
``````

The error shown is as follows：

Error using sparse
Indexes exceed the matrix dimension.

Error pretransfo ( line 444 )
dblks = cumsum ( full ( sparse ( 1, istrt, 1,1, sdpL ) ) ) ;

error sedumi ( line 261 )
[ A, b, c, K, prep, origcoeff ] = pretransfo ( A, b, c, K, pars ) ;

Error cvx _ run _ solver ( line 50 )
[ varargout { 1 : nargout } ] = sfunc ( input { : } ) ;

Error cvx _ sedumi > solve ( line 245 )
[ xx, yy, info ] = cvx _ run _ solver ( @ sedumi, At, b, c, K, pars, ’ xx ', ’ yy ', ’ info ', settings, 5 ) ;

Error cvxprob / solve ( line 253 )
[ x, status, tprec, iters2, y, z ] = shim.solve ( [ At, Anew2 ], [ b ; bnew ], c, cones, true, prec, solv. settings, eargs { : } ) ;

Error cvx _ end ( line 88 )
solve ( prob ) ;

Error Untitled2 ( line 99 )
cvx _ end

I set the breakpoint before the line cvx _ end, and the operation will not report errors. But as long as it runs to cvx _ end, the error will be reported.

Try re-open MATLAB. Please translate the error to English if convenient.

Thank you, but restart matlab is still wrong. Shows that the index exceeds the matrix dimension.

Perhaps another version of `sparse` (such as from YALMIP) other than the built-in MATLAB versions, is in your MATLAB path ahead of the CVX method.

What is the output from
`which -all sparse`

You can try moving all of the CVX items to the top of your MATLAB path. If that doesn’t resolve things, try reinstalling MATLAB in a new MATLAB session. And make sure to use CVX 2.2 (not 3.0beta).

Thank you for your comments. This is what I ’ m showing here. Do I need to move it ?

It looks o.k. My recommendation at this point is to try the reinstall of CVX 2.2 in a new session. it appears that something is screwed up in your installation.

Does it work with SDPT3? Mosek (if you have it)?

Thanks，but I didn 't install anything except cvx, my version is 2.2

Have you tried reinstalling CVX, in a new MATLAB session?

No, I ’ ll try it now.

I just tried, still showing the same mistake.

Please show us all output, not just the error message.

This is all its output, Chinese translation in the text

Try using Mosek. if that is not available, try installing CVXQUAD and its `exponential.m` replacement. CVXQUAD: How to use CVXQUAD's Pade Approximant instead of CVX's unreliable Successive Approximation for GP mode, log, exp, entr, rel_entr, kl_div, log_det, det_rootn, exponential cone. CVXQUAD's Quantum (Matrix) Entropy & Matrix Log related functions

Either of these ways will avoid CVX’s successive approximation method.

Thank you for your suggestion. I ’ ll try it.

Installing mosek still doesn 't solve the problem, will it be my code problem ?

Use Mosek as solver. Then show all your output, please. Also, if you don’t mind, maybe provide your code and data, for the purpose of reproducing.

Thank you for your comments.I just didn 't change SeDuMi in the code to mosek, but now there 's a new error : Cannot be converted from cvx to double.
All outputs are as follows：

The red word means :
The following error occurred when converting cvx to double :
Cannot be converted from cvx to double.

Error Untitled2 ( line 87 )
R5 ( 1,1 ) = chi * ( G1 * W _ W ( N, N ) * G1 ’ ) ;

My code is as follows
close all
clc;
clear all;
%*参数设置%
K=2;%用户数量
T=1;%总时间周期
M=6;%发射天线数
N=30;%反射元件数
chi=0.8;%能量转换效率
PkC=0.005;%7dbm
Pe=0.0015;%1.8dbm
PPS=0.05;%17dbm
PIR=0.05;%17dbm
delta=10^(-6);%delta^2=-80dbm
Pmax=0.316;%25dbm
Rkmin=0.5;%bits/Hz
% Imax=10^5;%迭代次数
% Lmax=10^5;%迭代次数
% epsilon=10^-5;%允许误差
d0=10;%PS到RIS距离
d1=60;%RIS到IR距离

d01=sqrt(85);%能量站PB到RIS的距离
d02=sqrt(104);%能量站PB到WD2的距离
d11=sqrt(5);%RIS到WD1的距离
d12=2;%RIS到WD2的距离
d21=59;%WD1到信息接收机IR的距离
d22=58;%WD2到信息接收机IR的距离

% H = rice_matrix1(M,N);%306维
% G = rice_matrix2(M,N);%30
1维
H=randn(30,6);
G=randn(30,1);
H1 = abs(real(H)d01.^(-2));%306
H2 = abs(real(H)d02.^(-2));%306
h1 = abs(real(G)d11.^(-2));%301
h2 = abs(real(G)d12.^(-2));%301
G1=h1’H1;%16
G2=h2’H2;%16
h(1)=d01^(-2);%PT到ST1之间的信道增益
h(2)=d02^(-2);%PT到ST2之间的信道增益
g(1)=d21^(-2);%ST1到SR1之间的信道增益
g(2)=d22^(-2);%ST2到SR2之间的信道增益

% H1(1)=h1.*H;%PS到RIS之间的信道增益
% H1(2)=h2.*H;%PS到RIS之间的信道增益
% G1=g(1).*G;%ST1到SR1之间的信道增益
% G2=g(1).*G;%ST2到SR2之间的信道增益

opt_q1=[];
% opt_q2=[];
% opt_q3=[];
q1=10;
% q2=10;
% q3=20;

% gama=0.05;%Rn_h的中断概率
% rou=0.05;%Rn_b的中断概率
% delta_h=0.005;%Δhk的界
% delta_g=0.005;%Δgk的界

for l = 1:1:10
eta= 0.1;
% for k=1:K
% h_(1,k)=(4delta_hdelta_grou+2g(k)delta_h-2delta_hdelta_g)/log((h(k)+delta_h)/(h(k)-delta_h));
% g_(1,k)=g(k)+2
gama*delta_g-delta_g;
% end

%子问题1*
cvx_solver Mosek
cvx_begin
variable W_W(N,N) semidefinite
variables t0 t(K,1) p_p(K,1);
expressions R1(1,K) R2(1,K)
% R2(K,1) R3(K,1) R4(K,1) R5(K,1)
for k=1:K
%R1(k,1)=t(k,1)log(1+p_p(k,1)g(k)^2/(t(k,1)delta^2))/log(2);
R1(1,K)=-rel_entr(t(k,1),p_p(k,1)g(k)^2/delta^2+t(k,1));
%y=t(k,1),x=p_p(k,1)g(k)^2/delta^2;
end
% for k=1:K
% R2(k,1)=p_p(k,1);
% R3(k,1)=(t0+t(k,1))PkC;
% R4(k,1)=t(k,1)PIR;
% end
R5(1,1)=chi
(G1
W_W(N,N)G1’);
R5(1,2)=chi
(G2
W_W(N,N)G2’);
maximize sum(R1)-eta
(sum(p_p(k,1))+t0
(PPS+N
Pe)+t0
PkC+sum(t(k,1))PkC+sum(t(k,1))PIR+trace(W_W(N,N))-sum(R2));
subject to
t0+sum(t)<=T;
t0>=0;
trace(W_W(N,N))<=t0
Pmax;
for k=1:K
t(k,1)>=0;
p_p(k,1)+PkC
(t0+t(k,1))<=chi
(G1*W_W(N,N)*G1’);
R1(1,k)>=Rkmin;
end

cvx_end
q1=sum(R1)/(sum(p_p(k,1))+t0*(PPS+NPe)+t0PkC+sum(t(k,1))*PkC+sum(t(k,1))*PIR+trace(W_W(N,N))-sum(R2));
opt_q1=[opt_q1 q1];

end

oh，I seem to be running successfully！

Yes, the bug in your code.