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
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
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.
0 个评论
更多回答(1 个)
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
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)
[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);
disp([origins Destinations] == string('LAX'));
disp(all([origins Destinations] == string('LAX'),2));
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Linear Programming and Mixed-Integer Linear Programming 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!