MATLAB Answers

# exceeded number of array elements

2 views (last 30 days)
Maurilio Matracia on 22 Oct 2020
Commented: Stephen Cobeldick on 22 Oct 2020
Hello everyone,
could you please help me figure out how to fix this error? It is showing up already for B=1 and C=1
Here's the code:
clear,
% [ THIS IS test_I_analysis ]
%% PARAMETERS
H = [100 ]'; h=1 ; var = 2e5 ;
lambdaA = 1e-6 ; lambdaT = 0.15 ;
lambda = [lambdaA, lambdaA, lambdaT] ;
Sa = [4.88 9.6117 12.0810 27.304]' ; Sb = [0.429 0.1581 0.1139 0.0797]' ;
alpha = [2, 3, 3] ;
alpha= [2.37 3 3]
tau = 0.31623 ; sigma_n2 = 10^-13 ;
p = [1.585, 1.585, 10] ;
eta = [0.9972 0.9943 1]
xi = eta .* p ;
m = [2 1 1] ; % Shape parameters for Nakagami fadings
Ru=600
R= 500 ;
for A=1:2
dd{A,3} = @(d) (xi(3) / xi(A))^(1/alpha(3)) * d.^(alpha(A) / alpha(3)) ;
dd{3,A} = @(d) ( (xi(A) / xi(3))^(1/alpha(A)) * d.^(alpha(3) / alpha(A)) ) .* ( d>=dd{A,3}(H) ) ...
+ H .* ( d<dd{A,3}(H) ) ;
end
dd{2,1} = @(d) (xi(1) / xi(2))^(1/alpha(1)) * d.^(alpha(2) / alpha(1)) ;
dd{1,2} = @(d) ( (xi(2) / xi(1))^(1/alpha(2)) * d.^(alpha(1) / alpha(2)) ) .* ( d>=dd{2,1}(H) ) ...
+ H .* (d<dd{2,1}(H)) ;
d = @(z) sqrt(z.^2 + H^2) ;
for C = 1:3
dd{C,C} = @(z) z ;
for B = 1:3
D{B,C} = @(z) sqrt( dd{B,C}(d(z)).^2 - H^2 ) ;
D{B,3} = @(z) dd{B,3}(d(z)) ;
end
D{3,C} = @(z) sqrt(dd{3,C}(z).^2 - H^2) ;
D{C,C} = @(z) z ;
end
xm = @(Beta) Ru(1)*cos(Beta) - sqrt(R^2 - Ru(1)^2*sin(Beta).^2) ;
BetaX = asin(R/Ru(1)) ;
for u = 1:2
xX{u} = @(Beta) Ru(u)*cos(Beta) + sqrt( R^2 - (Ru(u)*sin(Beta)).^2 ) ;
Betai{u} = @(z) real( (z>=0).*acos( (z.^2 + Ru(u)^2 - R^2) ./ ( (z>0)*2*z*Ru(u) + (z==0)) ) ) ;
end
PLoS = @(z) 1 ./ (1+Sa(1)*exp( -Sb(1)*(180/pi*atan(H./z)-Sa(1)) ) ) ;
integrandK{1} = @(z) z ./ (1+Sa(1)*exp( -Sb(1)*(180/pi*atan(H./z)-Sa(1)) ) ) ;
integrandK{2} = @(z) z .* (1 - 1/(1+Sa(1)*exp( -Sb(1)*(180/pi*atan(H./z)-Sa(1)) ) ) ) ;
for A=1:2
I_{A} = @(s,z) (1 - (m(A)/(m(A) + s .* xi(A) .* (d(z)).^-alpha(A)) ).^m(A) ) .* integrandK{A}(z) ;
for B = 1:3
LapI{B,A} = @(s,x) exp(-2*pi*lambda(A) * integral(@(z) I_{A}(s, z), D{B,A}(x),Ru(u)-R,'ArrayValued',1) ) ...
.* exp(lambda(A)*integral(@(Beta) integral(@(z) I_{A}(s,z), xm(Beta),xX{1}(Beta),'ArrayValued',1), -BetaX,BetaX,'ArrayValued',1 ) ) ;
LapBC{B,A,1} = @(s,x) LapI{B,A}(s, x) .*(Ru(1)-R>D{B,A}(x)) ;
end
end
for u = 1:length(Ru)
r2{u} = @(Beta,z) Ru(u)^2 + z.^2 - 2*Ru(u)*z.*cos(Beta) ;
I_T{u} = @(Beta,s,z) exp(-r2{u}(Beta,z)./(2*var)) .* (1-(m(3)/(m(3)+s.*xi(3).*z.^-alpha(3))).^m(3)) .* z ;
for B=1:3
LapBC{B,3,u} = @(s,x) exp(-lambda(3)/sqrt(2*pi*var) * integral(@(z) integral(@(Beta) I_T{u}(Beta,s,z), 0,2*pi,'ArrayValued',1), D{B,3}(x), Ru(u)+R,'ArrayValued',1) ) ;
LapB{B,u} = @(s,x) LapBC{B,1,u}(s,x) .* LapBC{B,2,u}(s,x) .* LapBC{B,3,u}(s,x) ;
end
end
% [THIS IS test_I]
u=1;
DsI = [0 0 0] ;
xLapI = zeros(3);
LapBC_ = {1 1 1; 1 1 1; 1 1 1}
for B=1:2
for C = 1:2
LapBC_{B,C} = LapBC{B,C,u}(tau/xi(B)*DsI(B)^alpha(B), xLapI(B,C)) ;
end
end

#### 1 Comment

Maurilio Matracia on 22 Oct 2020
The weird thing is that I am actually able to pone s = 0 and x=0 to compute LapI{1,1}(s,x), but I then get the error when I implement it in the for loop

Sign in to comment.

### Answers (1)

Stephen Cobeldick on 22 Oct 2020
Ru is scalar, but in multiple locations you try to access Ru(2), e.g.:
for u = 1:2
xX{u} = @(Beta) Ru(u)*cos(Beta) + sqrt( R^2 - (Ru(u)*sin(Beta)).^2 ) ;
% ^^^^^ ^^^^^
and
LapI{B,A} = @(s,x) exp(-2*pi*lambda(A) * integral(@(z) I_{A}(s, z), D{B,A}(x),Ru(u)-R,'ArrayValued',1) ) ...
% ^^^^^

#### 2 Comments

Maurilio Matracia on 22 Oct 2020
Thank you very much! What did you exactly do to find out where the mistake was?
Stephen Cobeldick on 22 Oct 2020
Funnily enough I used Octave, because it gives an error message which names the exact variable related to the error:
̀error: Ru(2): out of bound 1
error: called from
temp0>@<anonymous> at line 56 column 23
temp0>@<anonymous> at line 58 column 39
temp0 at line 78 column 17
The MATLAB Editor is great for most debugging, but for cases like this just the line number is not enough.

Sign in to comment.

### Community Treasure Hunt

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

Start Hunting!