Filter löschen
Filter löschen

Index is out of range for deletion ???

3 Ansichten (letzte 30 Tage)
Mamali
Mamali am 27 Mai 2014
Kommentiert: Image Analyst am 28 Mai 2014
Hello guys. I get an error when I run the code pasted below.
error:
Matrix index is out of range for deletion.
Error in topo (line 194) path1(:,:,g)=[];
HERE IS THE CODE:
[finalLinkCost1DMatrix, path, corelation] = getSetOfPlanes(topology,realCapacityTopology,costTopology,AR,GW,X(x));
path1=path; % path(6,19,finalLinkCost1DMatrix)
for g = size(finalLinkCost1DMatrix,3):-1:1
for f = 1:size(AR,2) % 1:6
for h=1:size(finalLinkCost1DMatrix,2) % 1:19
if path(f,h,g)~=0
path1(:,:,g)=[];
finalLinkCost1DMatrix(:,:,g)=[];
HopCountLimitRequired=CheckHopNumber(finalLinkCost1DMatrix)
if HopCountLimitRequired==1
HopCount=h;
break
else
continue
end
end
end
end
end
size(finalLinkCost1DMatrix,3) is my final answer.
HopCountLimitRequired=CheckHopNumber(finalLinkCost1DMatrix) is a BOOL function that checks whether size(finalLinkCost1DMatrix,3) meets certain criteria(e.g <6 && >3)
I would really appreciate if you can help me correct my approach.
  3 Kommentare
Mamali
Mamali am 28 Mai 2014
Thanks. I will certainly follow the instructions from now on.
Image Analyst
Image Analyst am 28 Mai 2014
One other tip. Before you paste in here, highlight your code in MATLAB, then type Ctrl-i to properly indent your code . Then paste in here, highlight, then and only then click the {}Code button.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

dpb
dpb am 28 Mai 2014
for g = size(finalLinkCost1DMatrix,3):-1:1
for f = 1:size(AR,2) % 1:6
for h=1:size(finalLinkCost1DMatrix,2) % 1:19
if path(f,h,g)~=0
path1(:,:,g)=[];
finalLinkCost1DMatrix(:,:,g)=[];
...
As soon as you remove the plane g, anything else is invalid and there's no point in continuing in the loops on f and h (and, in fact, it's going to error if there's a second point in the path array as you've already discovered).
BTW, path is a Matlab m-file function; not a good idea to alias functions; I'd suggest finding a new name for it.
Since it appears that you're testing every value in each plane, it would be far more efficient and remove a lot of your looping issues of leaving a triply-nested loop if you wrote
for g = size(finalLinkCost1DMatrix,3):-1:1
if any(path(:,:,g)
path1(:,:,g)=[];
finalLinkCost1DMatrix(:,:,g)=[];
% other stuff needed for this plane goes here
...
continue
end
end

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by