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 次查看(过去 30 天)
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 个评论
Walter Roberson
Walter Roberson 2022-1-2
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.

请先登录,再进行评论。

采纳的回答

John D'Errico
John D'Errico 2022-1-3
编辑:John D'Errico 2022-1-3
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.

更多回答(1 个)

Walter Roberson
Walter Roberson 2022-1-2
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 个评论
Voss
Voss 2022-1-2
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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Linear Programming and Mixed-Integer Linear Programming 的更多信息

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by