Good evening , I would like to know how to compare bet. two strings if they are equal or not while having a counter to keep track of equalities , given a list of airports

1 visualización (últimos 30 días)
clear
clc
m="LAX";
X1=(xlsread('Set of Flights','Sheet2','C4:C45')); % List of Flights
SizeX1=max(size(X1));
[num1,txt1]=xlsread('Set of Flights','Sheet2','C4:D45'); % List of Origins
txt1=string(txt1);
origins=(txt1)
[num2,txt2]=xlsread('Set of Flights','Sheet2','E4:E45'); % List of Destinations
txt2=string(txt2);
Destinations=(txt2)
%X2=transpose(xlsread('Set of Flights','Sheet3','I4:I45'));
LAXd =0 ;
for i = 1 : SizeX1
if isequal(origins(i),m) && isequal(Destinations(i),m)
LAXd =LAXd+1;
else
LAXd=0;
end
end
fprintf('LAX = %.f \n\n ',LAXd);
  4 comentarios
Walter Roberson
Walter Roberson el 2 de En. de 2022
We don't know... these might be a record of bookings, and the task might have to do with counting round trips that begin and end at LAX.

Iniciar sesión para comentar.

Respuesta aceptada

John D'Errico
John D'Errico el 3 de En. de 2022
Editada: John D'Errico el 3 de En. de 2022
Easy. We see the set of flights. I've coupled the origins and destinations together here:
[origins,Destinations]
ans =
42×2 string array
"LAX" "JFK"
"LAX" "JFK"
"LAX" "JFK"
"SFO" "JFK"
"SFO" "JFK"
"ORD" "JFK"
"ORD" "JFK"
"ORD" "JFK"
"ATL" "JFK"
"MIA" "JFK"
"BOS" "JFK"
"BOS" "JFK"
"IAD" "JFK"
"IAD" "JFK"
"IAD" "JFK"
"JFK" "LAX"
"JFK" "LAX"
"JFK" "LAX"
"JFK" "SFO"
"JFK" "SFO"
"JFK" "ORD"
"JFK" "ORD"
"JFK" "ORD"
"JFK" "ATL"
"JFK" "MIA"
"JFK" "BOS"
"JFK" "BOS"
"JFK" "IAD"
"JFK" "IAD"
"JFK" "IAD"
"SFO" "JFK"
"ATL" "JFK"
"ATL" "JFK"
"MIA" "JFK"
"MIA" "JFK"
"BOS" "JFK"
"JFK" "SFO"
"JFK" "ATL"
"JFK" "ATL"
"JFK" "MIA"
"JFK" "MIA"
"JFK" "BOS"
Your test only increments LAXd in the circumstance that BOTH the origin and the destination is LAX.
if isequal(origins(i),m) && isequal(Destinations(i),m)
LAXd =LAXd+1;
else
LAXd=0;
end
Otherwise, LAXd is set to zero. Personally, I'm not surprised that no flights both start and end at LAX. It would be a quick flight, but hardly worth the effort. I wonder what such a flight would cost anyway?
I wonder if possibly you wanted to count if EITHER the origin OR the destination is LAX? In that case, the fix is trivial. Just use the or operator, thus ||, not &&. Or possibly LAXd only counts the number of flights that end at LAX. Again, pretty simple. You just need to use the correct test.

Más respuestas (1)

Walter Roberson
Walter Roberson el 2 de En. de 2022
if isequal(origins(i),m) && isequal(Destinations(i),m)
LAXd =LAXd+1;
else
LAXd=0;
end
Every time you encounter something in which the LAX is not matched, you reset the count to 0.
  4 comentarios
Voss
Voss el 2 de En. de 2022
Avoiding resetting LAX to 0 is correct, but in this case you get LAX == 0 because there are no flights that begin and end at LAX.
clear
clc
m="LAX";
X1=(xlsread('Set of Flights','Sheet2','C4:C45')); % List of Flights
SizeX1=max(size(X1));
[num1,txt1]=xlsread('Set of Flights','Sheet2','C4:D45'); % List of Origins
txt1=string(txt1);
origins=(txt1)
origins = 42×1 string array
"LAX" "LAX" "LAX" "SFO" "SFO" "ORD" "ORD" "ORD" "ATL" "MIA" "BOS" "BOS" "IAD" "IAD" "IAD" "JFK" "JFK" "JFK" "JFK" "JFK" "JFK" "JFK" "JFK" "JFK" "JFK" "JFK" "JFK" "JFK" "JFK" "JFK"
[num2,txt2]=xlsread('Set of Flights','Sheet2','E4:E45'); % List of Destinations
txt2=string(txt2);
Destinations=(txt2)
Destinations = 42×1 string array
"JFK" "JFK" "JFK" "JFK" "JFK" "JFK" "JFK" "JFK" "JFK" "JFK" "JFK" "JFK" "JFK" "JFK" "JFK" "LAX" "LAX" "LAX" "SFO" "SFO" "ORD" "ORD" "ORD" "ATL" "MIA" "BOS" "BOS" "IAD" "IAD" "IAD"
%X2=transpose(xlsread('Set of Flights','Sheet3','I4:I45'));
LAXd =0 ;
for i = 1 : SizeX1
if isequal(origins(i),m) && isequal(Destinations(i),m)
LAXd =LAXd+1;
% else
% LAXd=0;
end
end
fprintf('LAX = %.f \n\n ',LAXd);
LAX = 0
disp([origins Destinations] == string('LAX'));
1 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
disp(all([origins Destinations] == string('LAX'),2));
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

Iniciar sesión para comentar.

Categorías

Más información sobre Linear Programming and Mixed-Integer Linear Programming en Help Center y File Exchange.

Productos


Versión

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by