Index exceeds matrix dimensions.

2 views (last 30 days)
syed ali
syed ali on 1 Apr 2018
Answered: Walter Roberson on 3 Apr 2018
Coverage.m function [Ncoverage,Cov]=Coverage(LocateSensor,nGrid,r,re,Lambda,Beta,Cthreshold)
noP=size(LocateSensor,1);
idim=size(LocateSensor,2);
for inoP=1:noP
n=0;
for dim=1:2:idim
x=LocateSensor(inoP,dim);
y=LocateSensor(inoP,dim+1);
for i=1:nGrid
for j=1:nGrid
D_Euclidean=sqrt((x-i)^2+(y-j)^2);
if D_Euclidean>=r+re
Cij=0;
elseif D_Euclidean<=r-re
Cij=1;
else
a=D_Euclidean-(r-re);
Cij=exp(-Lambda*(a^Beta));
end
if Cij>=Cthreshold
n=n+1;
end
end
end
end
Cov=Cij;
Ncoverage(inoP,1)=n/(nGrid^2);
end
PSO.m
clear all close all clc
% Define the details of the table design problem nSensor = 20; nVar = nSensor.*2; nGrid = 100; ub = nGrid; lb = 1; r=5; re = 3; Lambda = .5; Beta = .5; Cthreshold = .7; fobj = @Coverage;
% Define the PSO's paramters noP = 100; maxIter = 20; wMax = 0.9; wMin = 0.4; c1 = 2; c2 = 2; vMax = (ub - lb); vMin = -vMax;
% The PSO algorithm
% Initialize the particles for k = 1 : noP Swarm.Particles(k).X = (ub-lb) .* rand(1,nVar) + lb; Swarm.Particles(k).V = zeros(1, nVar); Swarm.Particles(k).PBEST.X = zeros(1,nVar); Swarm.Particles(k).PBEST.O = inf;
Swarm.GBEST.X = zeros(1,nVar);
Swarm.GBEST.O = inf;
end
% Main loop for t = 1 : maxIter
% Calcualte the objective value
for k = 1 : noP
currentX = Swarm.Particles(k).X;
Swarm.Particles(k).O = fobj(currentX,nGrid,r,re,Lambda,Beta,Cthreshold);
% Update the PBEST
if Swarm.Particles(k).O < Swarm.Particles(k).PBEST.O
Swarm.Particles(k).PBEST.X = currentX;
Swarm.Particles(k).PBEST.O = Swarm.Particles(k).O;
end
% Update the GBEST
if Swarm.Particles(k).O < Swarm.GBEST.O
Swarm.GBEST.X = currentX;
Swarm.GBEST.O = Swarm.Particles(k).O;
end
end
% Update the X and V vectors
w = wMax - t .* ((wMax - wMin) / maxIter);
for k = 1 : noP
Swarm.Particles(k).V = w .* Swarm.Particles(k).V + c1 .* rand(1,nVar) .* (Swarm.Particles(k).PBEST.X - Swarm.Particles(k).X) ...
+ c2 .* rand(1,nVar) .* (Swarm.GBEST.X - Swarm.Particles(k).X);
% Check velocities
index1 = find(Swarm.Particles(k).V > vMax);
index2 = find(Swarm.Particles(k).V < vMin);
Swarm.Particles(k).V(index1) = vMax(index1);
Swarm.Particles(k).V(index2) = vMin(index2);
Swarm.Particles(k).X = Swarm.Particles(k).X + Swarm.Particles(k).V;
% Check positions
index1 = find(Swarm.Particles(k).X > ub);
index2 = find(Swarm.Particles(k).X < lb);
Swarm.Particles(k).X(index1) = ub(index1);
Swarm.Particles(k).X(index2) = lb(index2);
end
outmsg = ['Iteration# ', num2str(t) , ' Swarm.GBEST.O = ' , num2str(Swarm.GBEST.O)];
disp(outmsg);
dbstop if error
cgCurve(t) = Swarm.GBEST.O;
end
semilogy(cgCurve); xlabel('Iteration#') ylabel('Coverage')
  3 Comments
Stephen23
Stephen23 on 2 Apr 2018
@syed ali: please show us the complete error message. This means all of the red text.
syed ali
syed ali on 3 Apr 2018
Edited: syed ali on 3 Apr 2018
@Stephen @Bob
Getting error at this position with red text as index exceeds matrix dimensions.
% Check velocities
index1 = find(Swarm.Particles(k).V > vMax);
index2 = find(Swarm.Particles(k).V < vMin);
Swarm.Particles(k).V(index1) = vMax(index1);
Swarm.Particles(k).V(index2) = vMin(index2);

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 3 Apr 2018
Your vMax is constructed from scalars so it is a scalar. But you try to index it under the assumption that it is the same length as the particles.

Community Treasure Hunt

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

Start Hunting!