MATLAB: Subscript indices must either be real positive integers or logicals. New to MATLAB please help me out.

error in (line 89) oets(ki)).^2))i)=s*(et+eamp*(distances(kjopo_pop(j

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');
%
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(A,B, 'mo-', 'MarkerSize', 10,'LineWidth', 2);
hold on
% Make 75 points in set #2
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
% Find min distance
disp (distances);
dist1 = sqrt((x2-A).^2+(y2-B).^2)
%disp (dist1);
dist2 =sqrt((x1-A).^2+(y1-B).^2)
disp (minDistance);
a=1:40;
for i = 1:10
pop(i,:) = a(randperm(numel(a),10))
end
for i = 1:10
opo_pop(i,:) = 40-pop(i,:)
end
%Est(13,5)=zeros();
%for i=1:13
% b=1;
%for k=1:50
%for j=10*k-9:10*k
%if mod(k,10)==0
%b=b+1;
%end
s=250;
Et=16.7;
Eamp=1.97;
N=10;
data_agg=1;
Er = 36.1;
d0=30;
c=10;
for k = 1:13
for j = 1:10
for i = 1:10
Ets(k,j,i)=s*(Et+Eamp*(distances(k,pop(j,i)).^2));
%Es(i,b)=Est(i,b)+Et(i,k);

end
end
end
disp (Ets);
for k = 1:13
for j = 1:10
for i = 1:10
OEts(k,j,i)=s*(Et+Eamp*(distances(k,opo_pop(j,i)).^2));
%Es(i,b)=Est(i,b)+Et(i,k);
end
end
end
disp (OEts);
%not able to get the output

Best Answer

With some guessing:
a = 1:40;
for i = 1:10
pop(i,:) = a(randperm(numel(a),10))
end
for i = 1:10
opo_pop(i,:) = 40 - pop(i,:)
end
If a is between 1 and 40 and you calculate 40 - pop, the result opo_pop is between 0 and 39. Then:
distances(k,opo_pop(j,i))
will fail, if a 0 is found, because Matlab's indices start at 1. Solution:
opo_pop(i,:) = 41 - pop(i,:)
Now opo_pop is between 1 and 40 also.