MATLAB: ??? Matrix index is out of range for deletion…

matrix index is out of range for deletion

[finalLinkCost1DMatrix, path, corelation] = getSetOfPlanes(topology,realCapacityTopology,costTopology,AR,GW,X(x));
path1=path;
finalLinkCost1DMatrix1=finalLinkCost1DMatrix;
for k=1:size(finalLinkCost1DMatrix,3)
for f=1:6
if path(f,8,k)~=0
path1(:,:,k)=[];
finalLinkCost1DMatrix(:,:,k)=[];
break;
else
continue;
end
end
end
I get
??? Matrix index is out of range for deletion.
Error in ==> topo at 76 path1(:,:,k)=[]; Can anyone help me with this. with thanks

Best Answer

Trivial example implemented two ways--
a) your case (from beginning)...
X=rand(10,1);
for i=1:length(X)
if(X(i)<0.5)
X(i)=[];
end
end
b) correctly (the first alternative given earlier)...
X=rand(10,1);
for i=length(X):-1:1
if(X(i)<0.5)
X(i)=[];
end
end
It's same thing excepting for a 1D array instead of 3D and one small array so you can use debugger if need to to understand the difference.