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

4 次查看(过去 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 个评论

请先登录,再进行评论。

采纳的回答

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 个评论

请先登录,再进行评论。

Community Treasure Hunt

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

Start Hunting!

Translated by