how can i solve this probleme :??? Attempted to access degree(100); index out of bounds because numel(degree)=1

1 view (last 30 days)
hello i have this msg :
??? Attempted to access degree(100); index out of bounds because numel(degree)=1.
Error in ==> untitled>pushbutton4_Callback at 397
degree(i)=degree(i)+1;% CALCULE LEs nombre de neighbord( DEGRE) DES NOEUDS
REDONDANTS
with this code :
degree=[];
if isfield(handles,'net')
for i = 1:numel(handles.net(1,:))
degree(i)=0;
for j = 1:numel(handles.net(1,:))
X1 = handles.net(2,i);
Y1 = handles.net(3,i);
X2 = handles.net(2,j);
Y2 = handles.net(3,j);
xSide = abs(X2-X1);
ySide = abs(Y2-Y1);
d = sqrt(xSide^2+ySide^2);% distance euclidienne
DD(:,i)=d;
%disp(DD);
%RESORTIR LES NOEUD REDONDANTS
if (d<=2*(handles.r))&&(i~=j)
degree(i)=degree(i)+1;% CALCULE LEs nombre de neighbord( DEGRE) DES NOEUDS REDONDANTS
disp(degree(i));
plot([X1,X2],[Y1,Y2],'o','LineWidth',0.1);
% ((X1,Y1),(X2,Y2))= mat_redondant;
%disp(mat_redondant);
hold on;
for i = 1:numel(handles.net(1,:))
for j = 1:numel(handles.net(1,:))
if (d<=2*(handles.r))&&(i~=j)
if (max(degree))&&(min(handles.d_sink))
plot([X1,X2],[Y1,Y2],'o','LineWidth',0.1);
end
end
end
end

Accepted Answer

Geoff Hayes
Geoff Hayes on 7 Nov 2015
Edited: Geoff Hayes on 7 Nov 2015
ali - you may want to verify that the above code is correct because from the looks of it, there are four for loops that are using duplicated indexing variables. I recommend that you use different indexing variables for all of your for loops, and try to avoid using i and j since MATLAB uses them to represent the imaginary number.
As for your error message, it is telling you that you are trying to access the 100th element in an array that is 1x1. I suspect that is is because of your third for loop using an indexing variable of i which is the same as the outer for loop. Consider the following example
for m=1:10
for n=1:10
fprintf('m=%d n=%d\n',m,n);
for m=1:5
end
end
end
If we consider the output from the first handful of iterations of the inner for loop we see that
m=1 n=1 % this is correct for m and n
m=5 n=2 % m is incorrect!! should be 1 still!!
m=5 n=3
m=5 n=4
m=5 n=5
m=5 n=6
m=5 n=7

More Answers (0)

Community Treasure Hunt

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

Start Hunting!