Indexed exceeds matrix dimensions
1 view (last 30 days)
Show older comments
Sushree Patra
on 5 Dec 2018
Commented: Walter Roberson
on 27 May 2020
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)
3 Comments
Accepted Answer
Walter Roberson
on 5 Dec 2018
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)
6 Comments
RAJENDRA KUMAR SHARMA
on 27 May 2020
Edited: Walter Roberson
on 27 May 2020
how to make objective function of coordination relay please anybody explain
Walter Roberson
on 27 May 2020
objective function of relay coordination should be asked as a new topic; http://www.mathworks.com/matlabcentral/answers/6200-tutorial-how-to-ask-a-question-on-answers-and-get-a-fast-answer
More Answers (1)
Luna
on 5 Dec 2018
Edited: Luna
on 5 Dec 2018
Your out and Ets variable is below:
out = [21 20 24 26 39 36 1 28 33 15] % 1x10 array
Ets = [.. ..] % 10x13 matrix
Ef_total = [.. ..] % 10x1 array
When you get in for loop for k = 1:10 and i =1:13, out(i) return to you the first, second, third element of the out array.
For example i = 1. out(i) will be 21. Your Ets only have 10 rows and 13 columns. You can't get the 21st column element you are asking for in bolded below. Because there is only 13 columns. Same for Ef_total.
(Ets(k,out(i)) will be Ets(k,21) ... etc.. % you get the idea)
The below formula in for loop definetely cause error.
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;
You should change them as follows but check if it does the correct calculation:
fitness(i,k)= (c*out(i)*1)+(c*(Ets(i,k)*1+Ef_total(i,1)))+(c*sqrt((distances(k,out(i))-d0).^2))+c;
5 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!