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

1 次查看(过去 30 天)
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
  1 个评论
Jan
Jan 2018-12-6
编辑:Jan 2018-12-6
Please post the complete error message, such that the readers do not have to guess, where the problem occurs.
There are many "hold on" commands in the code. One is sufficient.

请先登录,再进行评论。

采纳的回答

Jan
Jan 2018-12-6
编辑:Jan 2018-12-6
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.
  3 个评论
Sushree Patra
Sushree Patra 2018-12-7
one more doubt please help me.
After calculate Ets and OEts I want to calculate minimum fitness and minimum Ofitness value then union of them get the union i have to sort them and select 10 minimum value.
But the problem is when i used min function to calculate it dosen't calculate properly.
I am using min function in bold format
please check it out and let me know
Thank You
for j=1:10
for i=1:10
for k= 1:1
Etf(j,i,k)= N*data_agg*Et*(s*dist1(k,pop(j,i)))+N*s*data_agg*Et;
Erf = (N-1)*Er*s;
%Ef_total(i,k)=Etf(i,k)+Erf
end
end
end
Ef_total =Etf + Erf
for j=1:10
for i=1:10
for k= 1:1
OEtf(j,i,k)= N*data_agg*Et*(s*dist1(k,opo_pop(j,i)))+N*s*data_agg*Et;
OErf = (N-1)*Er*s;
%Ef_total(i,k)=Etf(i,k)+Erf
end
end
end
OEf_total =OEtf + OErf
for j=1:10
for i =1:10
for k = 1:13
fitness(j,i,k)= (c*pop(j,i)*1)+(c*(Ets(k,j,i)*1+Ef_total(j,i,1)*1))+(c*sqrt((distances(k,pop(j,i))-d0).^2))+(c*1);
end
end
minFit(j) = min(fitness(j,:));
end
disp(fitness);
for j=1:10
for i =1:10
for k = 1:13
Ofitness(j,i,k)= (c*opo_pop(j,i)*1)+(c*(OEts(k,j,i)*1+OEf_total(j,i,1)*1))+(c*sqrt((distances(k,opo_pop(j,i))-d0).^2))+(c*1);
end
end
OminFit(j) = min(Ofitness(j,:));
end
disp(Ofitness);
disp(minFit);
disp(OminFit);
Int_Fit = [minFit;OminFit]
Jan
Jan 2018-12-7
Please apply a proper formatting of teh code. This is easy: select the code with the mouse and press teh button for code formatting in teh bar on top of the field for writing the message. It improves the readability.
"it dosen't calculate properly" - I cannot know, what this means. Of couse the min() function determines the minimal element. Then what does "not properly" mean? If the results differ from your expectations, it is required to mention, what you expect and what you get.
(c*1) ? You multiply several times by 1, but this is a waste of time for the computations and for typing also.
I cannot run your code due to the lack of input values.
Please remember that it is hard to understand a sentence like this: "After calculate Ets and OEts I want to calculate minimum fitness and minimum Ofitness value then union of them get the union i have to sort them and select 10 minimum value." I have almost no chance to know, what this means.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Performance and Memory 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by