Index is out of range for deletion ???

3 次查看(过去 30 天)
Mamali
Mamali 2014-5-27
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 个评论
Mamali
Mamali 2014-5-28
Thanks. I will certainly follow the instructions from now on.
Image Analyst
Image Analyst 2014-5-28
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.

请先登录,再进行评论。

回答(1 个)

dpb
dpb 2014-5-28
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

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by