Index exceeds the number of array elements (1).

1 Ansicht (letzte 30 Tage)
Chun Yin Lui
Chun Yin Lui am 11 Apr. 2021
Kommentiert: Chun Yin Lui am 11 Apr. 2021
Can someone tell me where am I wrong here?
xm=400; %diameters of sensor network
ym=400;
size = 400;
L = size / 3;
sink.x=200; %distance of base station from the network
sink.y=200;
n = 100; %no of nodes
p=0.1; %probibilty of a node to become cluster head
X = [280 91 241 8 286 210 293 213 347 123 3 76 365 359 337 104 84 140 173 239 151 387 130 232 61 256 245 136 11 293 147 174 50 367 129 8 371 261 174 299 25 87 302 91 340 72 117 397 161 376 396 117 361 334 267 263 99 45 382 320 104 201 21 166 112 110 370 105 301 252 135 73 172 391 270 298 59 147 7 98 339 362 132 36 245 85 186 325 10 397 172 185 267 125 159 317 282 299 230 249];
Y = [80 361 40 119 360 311 71 370 337 78 186 118 273 94 274 292 37 230 89 36 77 399 2 127 280 103 126 118 232 171 212 368 16 172 45 91 66 186 139 307 320 255 103 1 340 270 9 134 307 207 8 128 184 222 204 267 225 286 389 367 154 116 111 312 156 341 341 94 116 63 394 333 240 165 70 30 388 115 330 345 214 262 385 144 327 272 97 5 47 212 179 95 316 223 397 196 158 318 393 115];
xy = reshape([X;Y],100,[]);
subplot(1,2,1)
plot(xy(:,1), xy(:,2), 'r.')
axis equal
grid on
xlim([0,400])
ylim([0,400])
title('raw data')
binEdges = linspace(1,400,4);
set(gca,'XTick',binEdges, 'YTick',binEdges)
% Compute density in 3X3 bins
subplot(1,2,2)
h = histogram2(xy(:,1), xy(:,2), binEdges,binEdges,...
'DisplayStyle','tile','ShowEmptyBins','on');
axis equal
grid on
xlim([0,400])
ylim([0,400])
title('Density')
set(gca,'XTick',0:100:400, 'YTick',0:100:400)
cb = colorbar();
ylabel(cb,'Density')
% To label counts
[xTxt, yTxt] = ndgrid(h.XBinEdges(2:end)-h.BinWidth(1)/2, ...
h.YBinEdges(2:end)-h.BinWidth(2)/2);
labels = compose('%.0f', h.Values);
hold on
text(xTxt(:), yTxt(:), labels(:), 'VerticalAlignment', 'Middle', 'HorizontalAlignment','Center')
A1 = (h.Values(1));
A2 = (h.Values(2));
A3 = (h.Values(3));
A4 = (h.Values(4));
A5 = (h.Values(5));
A6 = (h.Values(6));
A7 = (h.Values(7));
A8 = (h.Values(8));
A9 = (h.Values(9));
CCA1 = ceil(A1/10); % The number of chosen cluster head in Area 1
CCA2 = ceil(A2/10); % The number of chosen cluster head in Area 2
CCA3 = ceil(A3/10); % The number of chosen cluster head in Area 3
CCA4 = ceil(A4/10); % The number of chosen cluster head in Area 4
CCA5 = ceil(A5/10); % The number of chosen cluster head in Area 5
CCA6 = ceil(A6/10); % The number of chosen cluster head in Area 6
CCA7 = ceil(A7/10); % The number of chosen cluster head in Area 7
CCA8 = ceil(A8/10); % The number of chosen cluster head in Area 8
CCA9 = ceil(A9/10); % The number of chosen cluster head in Area 9
Eo=0.5; %energy supplied to each node
ETX=50*0.000000001; %transmiter energy per node
ERX=50*0.000000001; %reciever energy per mode
Efs=10*0.000000000001; %amplification energy when d is less than d0
Emp=0.0013*0.000000000001; %amplification energy when d is greater than d0
for i=1:1:n
if X(i) > 0 && X(i) < L && Y(i) > 0 && Y(i) < L
Efs1(i)=Efs/CCA1; % amp energy just for intra cluster communication.
Emp1(i)=Emp/CCA1;
end
if X(i) > L && X(i) < 2*(L) && Y(i) > 0 && Y(i) < L
Efs1(i)=Efs/CCA2; % amp energy just for intra cluster communication.
Emp1(i)=Emp/CCA2;
end
if X(i) > 2*(L) && X(i) < 3*(L) && Y(i) > 0 && Y(i) < L
Efs1(i)=Efs/CCA3; % amp energy just for intra cluster communication.
Emp1(i)=Emp/CCA3;
end
if X(i) > 0 && X(i) < L && Y(i) > L && Y(i) < 2*(L)
Efs1(i)=Efs/CCA4; % amp energy just for intra cluster communication.
Emp1(i)=Emp/CCA4;
end
if X(i) > L && X(i) < 2*(L) && Y(i) > L && Y(i) < 2*(L)
Efs1(i)=Efs/CCA5; % amp energy just for intra cluster communication.
Emp1(i)=Emp/CCA5;
end
if X(i) > 2*(L) && X(i) < 3*(L) && Y(i) > L && Y(i) < 2*(L)
Efs1(i)=Efs/CCA6; % amp energy just for intra cluster communication.
Emp1(i)=Emp/CCA6;
end
if X(i) > 0 && X(i) < L && Y(i) > 2*(L) && Y(i) < 3*(L)
Efs1(i)=Efs/CCA7; % amp energy just for intra cluster communication.
Emp1(i)=Emp/CCA7;
end
if X(i) > L && X(i) < 2*(L) && Y(i) > 2*(L) && Y(i) < 3*(L)
Efs1(i)=Efs/CCA8; % amp energy just for intra cluster communication.
Emp1(i)=Emp/CCA8;
end
if X(i) > 2*(L) && X(i) < 3*(L) && Y(i) > 2*(L) && Y(i) < 3*(L)
Efs1(i)=Efs/CCA9; % amp energy just for intra cluster communication.
Emp1(i)=Emp/CCA9;
end
end
%Data Aggregation Energy
EDA=5*0.000000001;
a=Eo/2; %The minimum energy lever of nodes to become cluster head
rmax=2500; %no. of rounds
for i=1:1:n
do(i)=sqrt(Efs(i)/Emp(i)); %distance between cluster head and base station
do1(i)=sqrt(Efs1(i)/Emp1(i));
end

Akzeptierte Antwort

VBBV
VBBV am 11 Apr. 2021
Bearbeitet: VBBV am 11 Apr. 2021
do=sqrt(Efs/Emp); %distance between cluster head and base station
for i=1:1:n
%do(i)=sqrt(Efs/Emp); %distance between cluster head and base station
do1(i)=sqrt(Efs1(i)/Emp1(i));
end
Efs and Emp are singular value
  3 Kommentare
VBBV
VBBV am 11 Apr. 2021
Bearbeitet: VBBV am 11 Apr. 2021
I didn't get any error if I put the first line out of for loop. See the updated one
Chun Yin Lui
Chun Yin Lui am 11 Apr. 2021
oh, I got it!! Thank you for your help.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by