MATLAB: Indexed exceeds matrix dimensions

error index exceeds matrix dimensions

clc;
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 25;
figure;
imshow('pc3.png');
hold on
hold on
%legend('Relay', 'Sink', 'Sensor');
% placed 13 node according to the co-ordinates
x1 = [105,107,58,58,75,146,118,18,48,55,108,80,80];
y1 = [190,280,190,280,145,153,103,153,103,65,65,50,10];
hold on
A=100;
B=140;
%plot the output node
plot(A,B, 'mo-', 'MarkerSize', 10,'LineWidth', 2);
hold on
% placed 50 point randomly
Xmin = 60;
Xmax = 99;
x2 = Xmin+rand(1,50)*(Xmax-Xmin);
Ymin = 10;
Ymax= 260;
y2 = Ymin+rand(1,50)*(Ymax-Ymin);
plot(x2, y2, 'b*');
%numPoints2 = 50;
%x2 = 167*rand(numPoints2, 1);
%y2 = 302*rand(numPoints2, 1);
% Plot set 1 in red
plot(x1, y1, 'r.', 'MarkerSize', 13);
% Plot set 2 in blue
hold on;
plot(x2, y2, 'b*', 'MarkerSize', 5);
grid on;
for i=1:13
for j=1:50
% Find distances between every point in set 1 to every point in set #2.
distances(i,j) = pdist2([x1(i),y1(i)], [x2(j), y2(j)], 'euclidean');
end
minDistance(i) = min(distances(i,:));
end
disp (distances);
dist1 = sqrt((x2-A).^2+(y2-B).^2)
%disp (dist1);
dist2 =sqrt((x1-A).^2+(y1-B).^2)
%disp (dist2);
% Find min distance
disp (minDistance);
a=1:50;
%choose 10 node randomly from 50 node
out=a(randperm(numel1(a),10))
s=250;
Et=16.7;
Eamp=1.97;
%caculate the energy consumption of 13 sensor to that choosen 10 random node which store in out
%?_? (?, ?_?? )=?(?t+ ???? ???^2)
for i=1:10
for k=1:13
Ets(i,k)=s*(Et+Eamp*(distances(k,out(i)).^2));
end
end
disp(Ets);
N=10;
data_agg=1;
Er=36.1;
%calculate the energy consumption of 10 relay node while transmiitting to 1 output device
%?_??=???? (?, ???/d??? ) )+????t
%?_??=(?−1) ?r(?)
%?_? (?)= ?r(?)
for i=1:10
for k= 1:1
Etf(i,k)= N*data_agg*Et*(s*dist1(k,out(i)))+N*s*data_agg*Et; %transmission energy of relay node
Erf=(N-1)*Er*s;%reception energy of relay node
Ef_total(i,k)=Etf(i,k)+Erf %total energy consumption of relay
end
end
d0=40;
c=10;
%find out fitness function of that 10 relay node eith respect to 13 sensor node
%Fitness =c*∑j∈R*cost(j)*xj +c*(∑i∈S,j∈R Ei * xij+∑j∈R xj*Er)+ c* √(∑i∈S,j∈R(dij-d0)^2 ))/(∑i∈S,j∈R xij)+c*(∑i∈S,j∈R xij )/(⋃i∈S,j∈R xij)
% xj=1,if relay exist otherwise 0
%S=13 sensor
%R=10 relay stored in out
for i=1:10
for k=1:13
fitness(i,k)= (c*out(i)*1)+(c*(Ets(k,out(i))*1+Ef_total(k,out(i))*1))+(c*sqrt((distances(k,out(i))-d0).^2))+c;
end
end
disp(fitness)

Best Answer

Ets is 10 by 13.
Your fitness calculation includes Ets(k,out(i)) . But out(i) can be up to 50 and k can be up to 13 . You probably want Ets(i,k)