for loop i doesn't increase

조회 수: 2 (최근 30일)
Charles-Antoine Danis
Charles-Antoine Danis 2020년 3월 22일
댓글: Charles-Antoine Danis 2020년 3월 22일
Do you see the problem why the i doesn't increase after the loop from the for
heres the code where the i never increment :
borne_actuel = borne1;
borne_ideal = 0;
distance_ideal = 0;
distance_ideal_fin = distance;
distance2 = 0;
distance3 = 0;
borne_invoulu = 0;
borne_precedente = borne1;
while 1
for i = 1:150
distance2 = matrixDistance(borne_actuel,i);
distance3 = matrixDistance(i,borne2);
if (distance2 <= autonomie && i ~= borne_invoulu)
if (i == borne2)
borne_ideal = i;
break
elseif (distance2 >= distance_ideal && distance3 < distance_ideal_fin)
distance_ideal = distance2;
distance_ideal_fin = distance3;
borne_ideal = i;
end
end
end
if (distance_ideal == 0.0)
fprintf('Aucun chemin ideal\n');
break
end
%ajouter la borne actuel dans un tableau pour suivre le chemin
% tab.ajouter[borne_ideal];
% test tab
if (borne_ideal == borne_actuel)
borne_invoulu = borne_ideal;
end
fprintf('%g\n', borne_ideal);
borne_precedente = borne_actuel;
borne_actuel = borne_ideal;
if (borne_actuel == borne2)
%on est arrivere
fprintf('GoodJob\n');
break
end
borne_ideal = borne1;
end
end
  댓글 수: 3
Charles-Antoine Danis
Charles-Antoine Danis 2020년 3월 22일
I already see something the command window display condtanstly the number of the input I enter. So for exemple I enter 11. The command window reply. This is what it happen like even if no fprintf is in rhe foor loop
11 11 11 11 11 11 ... And I have to do ctrl-c to be able to end the function
Charles-Antoine Danis
Charles-Antoine Danis 2020년 3월 22일
I coulldn't really put the code because it has like 20 called function probably so it will be hard to understand.
borne is like but i coould give you more information.
The program is to a person who has an electric car and it will input initial charging station and final charging station.
the program calculate and tell wich charging station depending on the autonomy of the car (autonomie).
this is the code complete
function [unChemin,cheminPossible] = OptimiserChemin ()
[tab,tf] = CreerTrajet ();% return [borne
borne1 = tab(1);
borne2 = tab(2);
autonomie = tab(3);
[matrix] = ChargerMatrice (1);
matrixDistance = matrix;
[matrix] = ChargerMatrice (2);
matrixDuree = matrix;
distance = matrixDistance(borne1,borne2);
duree = matrixDuree(borne1,borne2);
fprintf('%g\n%g\n%g\n',distance,duree,autonomie);
if (tf == 0)
cheminPossible = 0;
end
if (distance < autonomie)
%aller au point
end
borne_actuel = borne1;
borne_ideal = 0;
distance_ideal = 0;
distance_ideal_fin = distance;
distance2 = 0;
distance3 = 0;
borne_invoulu = 0;
borne_precedente = borne1;
while 1
for i = 1:150
distance2 = matrixDistance(borne_actuel,i);
distance3 = matrixDistance(i,borne2);
if (distance2 <= autonomie && i ~= borne_invoulu)
if (i == borne2)
borne_ideal = i;
break
elseif (distance2 >= distance_ideal && distance3 < distance_ideal_fin)
distance_ideal = distance2;
distance_ideal_fin = distance3;
borne_ideal = i;
end
end
end
if (distance_ideal == 0.0)
fprintf('Aucun chemin ideal\n');
break
end
%ajouter la borne actuel dans un tableau pour suivre le chemin
% tab.ajouter[borne_ideal];
% test tab
if (borne_ideal == borne_actuel)
borne_invoulu = borne_ideal;
end
fprintf('%g\n', borne_ideal);
borne_precedente = borne_actuel;
borne_actuel = borne_ideal;
if (borne_actuel == borne2)
%on est arrivere
fprintf('GoodJob\n');
break
end
borne_ideal = borne1;
end
end
the code of ChargerMatrice:
this code return a matrix of the distance between (bornes1,borne2)
function [matrix] = ChargerMatrice (choixOption)
if (choixOption == 1)
file = fopen('MatriceDistances.txt', 'rt');
%On s'assure que le fichier est disponible.
if file ~= -1
maintable1 = readmatrix('MatriceDistances.txt');
maintable1(1,:) = [];
maintable1(:,1) = [];
matrix = maintable1;
fclose(file);
else
fprintf('Une erreur est arrivée avec l''ouverture du fichier MatriceDistances.txt');
end
elseif (choixOption == 2)
file = fopen('MatriceDurees.txt', 'rt');
%On s'assure que le fichier est disponible.
if file ~= -1
maintable2 = readmatrix('MatriceDurees.txt');
maintable2(:,1) = [];
matrix = maintable2;
fclose(file);
else
fprintf('Une erreur est arrivée avec l''ouverture du fichier MatriceDurees.txt');
end
else
fprintf('Erreur! Veuillez choisir un nombre entre 1 et 2 correspondant à la matrice à afficher');
end
end

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by