how to make a comparison in a for loop. Problem on indexes

At the end of the for loop l want to compare the values of I(i) and I(j) look at the second for loop. here is the returned error :
??? Attempted to access I(2); index out of bounds because numel(I)=1.
Error in ==> eliminationphase at 143
if (I(i)==I(j)&&(j < i))
Look at the code below:
alpha= 10;
I=1;
for i=1:N
if (~ismember(num2str(i), num2str(F)))
while (C(i) < alpha)
for j=1:N
if ((i~=j) && (ismember(j, neighbour_n{i})))
C(i)=C(i)+C(j);
display(['la somme des cardinaux de ', num2str(i), ' is ', num2str(C(i))]);
I=I+1;
display(['The number of iteration of ', num2str(i), ' is ', num2str(I)]);
end
end
end
if (~ismember(num2str(i), num2str(F)))
display(['the value of the node: ', num2str(i), ' at the end of while is ; ', num2str(C(i))]);
end
end
end
% how to compare betwwen I(i) and l(j)?
% l need to retreive the all the values of I and compare between them and between thein indices
for i=1:N
for j=1:N
if (((~ismember(num2str(i), num2str(F)))&&(~ismember(num2str(i), num2str(F))))&&(i~=j))
if (I(i)==I(j)&&(j < i))
display(i);
end
if (I(i) >I(j))
display(i);
end
end
end
end

4 comentarios

mazari - use the debugger and step through the code to see why I is just a scalar and never becomes an array. Given that you haven't indicated what N, F, or C (etc.) are then it becomes difficult for others to try to debug the problem.
The error message is clear on what the problem is - you are expecting I to be a matrix or an array and instead it is just a scalar. When you run the above code, do you ever see any of the output statements (those from the display calls)?
mazari ahmed
mazari ahmed el 14 de Mzo. de 2015
Editada: mazari ahmed el 14 de Mzo. de 2015
l have adjusted the first loop N=200; % the number of nodes F=deleted_node{i}; % the set of deleted nodes (i=1:N) C=lenght(neighbour_n{i}); %the number of neighbours of the node i. Yes Geoff l do agree with you l need I as a scalar.the idea is that each node i has its own value of I.to make a comparison of the values of I of different nodes l need to have the index for exemple I(1)=5 I(3)= 2 ...
l've changed the intruction I(i)=I(i)+1 by I=I+1 (in the first loop)and it works perfectly.But how can l compare the value of I(1) and I(2)... in the second loop ? I(i) and I(j)
Why do you have the following condition in the inner for loop of your second loop block
if (((~ismember(num2str(i), num2str(F)))&&(~ismember(num2str(i), num2str(F))))&&(i~=j))
What is the intent behind this code?
Finally you are right Geoff, it's working

Iniciar sesión para comentar.

Respuestas (1)

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Preguntada:

el 14 de Mzo. de 2015

Respondida:

el 9 de Abr. de 2015

Community Treasure Hunt

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

Start Hunting!

Translated by