problem in my matlab code 'Index in position 1 exceeds array bounds (must not exceed 1)'

Hello everyone,
please how can i solve this problem:Index in position 1 exceeds array bounds (must not exceed 1).
Error in Sphere (line 51)
SinrC(i,j)=Pcellular(i,j)*calculate_gain(PositionsBS(1),PositionsBS(2), PositionsC(i,1),PositionsC(i,2)
)/(Icv+Noise);
Error in GeneticAlgorithm (line 11)
population.Chromosomes(i).fitness = obj( population.Chromosomes(i).Gene(:) );
Error in Main (line 50)
[BestChrom] = GeneticAlgorithm (M , N, MaxGen , Pc, Pm , Er , Problem.obj , visualization).
This is the code where i have SinrC(i,j):
throughputC=zeros(C);
SinrC=zeros(C,RB);
for i=1:C
Icv=0;
for j=1:RB
if binc(i,j)==1
for v=1:V
if binv(v,j)==1
Icv=Icv+Pv(v,j)*calculate_gain(PositionsBS(1),PositionsBS(2), PositionsV(v,1),PositionsV(v,2) );
end
end
SinrC(i,j)=Pcellular(i,j)*calculate_gain(PositionsBS(1),PositionsBS(2), PositionsC(i,1),PositionsC(i,2) )/(Icv+Noise);
throughputC(i)= throughputC(i)+W*log2(1+ SinrC(i,j));
end
end

4 Comments

That code has a number of variables whose size we do not know, so it is difficult for us to say.
I note by the way tyat ou refer to throughputC(i) with a single subscript, after having defined throughputC=zeros(C ) . zeros(C ) defines a CxC matrix, equivalent to zeros(C,C) . This would not cause any problem in the part of the code you posted, but could potentially cause a problem in other parts of your code.
The problem appear whene i put a value of C greator than 1.
here:
PositionsC=rand(1,2);%*XX;
PositionsV=rand(1,2);%*XX;
PositionsBS=rand(1,2);%*XX;
% Problem preparation
C=2;
V=3;
RB=C;
whene C+, no problem it gives to me the value of fitness function but whene i put like here C=2 or plus, i have this problem:
Index in position 1 exceeds array bounds (must not exceed 1).
Error in Sphere (line 51)
SinrC(i,j)=Pcellular(i,j)*calculate_gain(PositionsBS(1),PositionsBS(2), PositionsC(i,1),PositionsC(i,2)
)/(Icv+Noise);
Error in GeneticAlgorithm (line 11)
population.Chromosomes(i).fitness = obj( population.Chromosomes(i).Gene(:) );
Error in Main (line 50)
[BestChrom] = GeneticAlgorithm (M , N, MaxGen , Pc, Pm , Er , Problem.obj , visualization)
whos PositionsC
Name Size Bytes Class Attributes
PositionsC 1x2 16 double global

Sign in to comment.

Answers (2)

You define PositionsC as a row vector but you access it as a column vector.

4 Comments

how can i access to PositionsC in order to solve the problem please.
% Problem preparation
C=2;
PositionsC=rand(C,2);%*XX;
PositionsV=rand(C,2);%*XX;
PositionsBS=rand(C,2);%*XX;
I still have this problem
Matrix dimensions must agree.
Error in Sphere (line 89)
c=a+b;
Error in GeneticAlgorithm (line 11)
population.Chromosomes(i).fitness = obj( population.Chromosomes(i).Gene(:) );
Error in Main (line 51)
[BestChrom] = GeneticAlgorithm (M , N, MaxGen , Pc, Pm , Er , Problem.obj , visualization)
c=a+b does not appear in the code you posted, so we can only make guesses about it.
One of my guesses is that you are using R2018a or earlier.

Sign in to comment.

THis is the code of c=a
=b
a=sum(throughputV);
b=sum(throughputC);
c=a+b;
I calculate throughputC and throughputV by using there instructions:
SinrC=zeros(C,RB);
for i=1:C
Icv=0;
for j=1:RB
if binc(i,j)==1
for v=1:V
if binv(v,j)==1
Icv=Icv+Pv(v,j)*calculate_gain(PositionsBS(1),PositionsBS(2), PositionsV(v,1),PositionsV(v,2) );
end
end
SinrC(i,j)=Pcellular(i,j)*calculate_gain(PositionsBS(1),PositionsBS(2), PositionsC(i,1),PositionsC(i,2) )/(Icv+Noise);
throughputC(i)= throughputC(i)+W*log2(1+ SinrC(i,j));
end
end
if throughputC(i)<SINRcth
constraintSatified=false;
end
end
throughputV=zeros(V);
SinrV=zeros(V,RB);
for v=1:V
Ivc=0;
for j=1:RB
if binv(i,j)==1
for c=1:C
if binc(c,j)==1
Ivc=Ivc+Pcellular(i,j)*calculate_gain(PositionsC(i,1),PositionsC(i,2), PositionsV(v,1),PositionsV(v,2) );
end
end
SinrV(v,j)=Pv(v,j)*calculate_gain(PositionsV(v,1),PositionsV(v,2),PositionsC(c,1),PositionsC(c,2) )/(Ivc+Noise);
throughputV(v)= throughputV(v)+W*log2(1+ SinrV(v,j));
disp( throughputV(v));
end
end
if throughputV(v)< SINRvth
constraintSatified=false;
end
end

2 Comments

Fishing through your postings as you do not show all of your code in one place, we see the lines
C=2;
V=3;
throughputC=zeros(C);
throughputV=zeros(V);
So throughputC is 2 x 2 and sum(throughputC) would be 1 x 2. Meanwhile throughputV is 3 x 3, and sum(throughputV) would be 1 x 3. You cannot add a 1 x 2 vector and a 1 x 3 vector.
I refer back to my first response,
I note by the way that you refer to throughputC(i) with a single subscript, after having defined throughputC=zeros(C ) . zeros(C ) defines a CxC matrix, equivalent to zeros(C,C) . This would not cause any problem in the part of the code you posted, but could potentially cause a problem in other parts of your code.
If you want a 1 x C vector, then you should be using zeros(1,C) not zeros(C )

Sign in to comment.

Asked:

on 27 Aug 2019

Commented:

on 1 Sep 2019

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!