Indexed 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)

3 Comments

Luna
Luna on 5 Dec 2018
Edited: Luna on 5 Dec 2018
Please define your question. You have too many syntax errors, end end together in the same line. Also, what is distances variable?
What do you want to get from this code?
distance is defined and this one is the part of code my problem is when i calculate the fitness function then it shows indexed exceeds matrxmatrix dimensions
due to shortage of space i put end in same line. please help me out.
if the code is not clear then i will send the total code

Sign in to comment.

 Accepted 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)

6 Comments

Thank you for you answer. out(i) is that select 10 node from 50 node randomly
my objective is to find out energy consumption of 13 predefined node with respect to selected 10 node which are stored in out.
the i have to find out Etf which is the energy consumption of that choosen 10 node to 1 out put node having distance and stored in dist2.
then calculate the fitness function
the value out(i) can be up to 50 because it is a random node number . You try to use it to index an array in a position that has 13 columns. You also try to use row number up to 13 even through the array has only 10 rows.
means out where i choose 10 number i have to choose 50 random number ?
which part i have to change can you please help me in my code
how to make objective function of coordination relay please anybody explain

Sign in to comment.

More Answers (1)

Luna
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

Thank you for the answer. But 21st is out(i) first element
Ets and Ef_total value will calculated
and if i increase the column value then its also showing same index exceeds matrix dimensions.
can you please help me solve it in my code where to do changes I am unable
yes i got the point.
Thank you very much
one more think as i placed the blue star in body randomly but they are not covered all over the body only in between the define range. also that 50 blue star distributed uniformly randomly
Did it fix your code?
yes it fixed in my code Screenshot_2018-12-05-20-23-42-008_com.android.chrome.png

Sign in to comment.

Categories

Community Treasure Hunt

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

Start Hunting!