Show the mean and standard Deviation values of each cell array member

1 view (last 30 days)
Hello!
I want to get the mean delay, standard deviation, max delay and min delay of each airport but the code that I made up is only returning the results for airport DFW.
Is there something wrong with my code?
F = dataset('xlsfile','Delta_Data');
% Airport Departure Statistics
Origin = {'CVG','ATL','ORD','LAX','MIA','DFW'};
for k = 1:6
Origin_Flights = find(strcmp(F.Origin,Origin(k)));
Number_Flights = length(Origin_Flights);
Mean_Delay = mean(F.Delay(Origin_Flights));
ST_Dev = std(F.Delay(Origin_Flights));
Max_Delay = max(F.Delay(Origin_Flights));
Min_Delay = min(F.Delay(Origin_Flights));
end
disp(Mean_Delay);
disp(ST_Dev);
disp(Max_Delay);
disp(Min_Delay);
Here is what I get when I run the code:
9.4474
42.5381
297
-10
How can I code it in such a way that it will show the results of each airport? I hope you could help me with this.
Thank you so much!

Accepted Answer

Tina
Tina on 15 Jan 2022
Hi there!
You did made a for loop but you are not using k anywhere in code except for origin flights.
If we assume that your data is in such way that first colunm has data of first airport and so on
then try this
for k = 1:6
Origin_Flights = find(strcmp(F.Origin,Origin(k)));
Number_Flights = length(Origin_Flights(k));
Mean_Delay = mean(F.Delay(Origin_Flights(k)));
ST_Dev = std(F.Delay(Origin_Flights(k)));
Max_Delay = max(F.Delay(Origin_Flights(k)));
Min_Delay = min(F.Delay(Origin_Flights(k)));
end
or by this
for k = 1:6
Origin_Flights = find(strcmp(F.Origin,Origin(:,k)));
Number_Flights = length(Origin_Flights(:,k));
Mean_Delay = mean(F.Delay(Origin_Flights(:,k)));
ST_Dev = std(F.Delay(Origin_Flights(:,k)));
Max_Delay = max(F.Delay(Origin_Flights(:,k)));
Min_Delay = min(F.Delay(Origin_Flights(:,k)));
end
  1 Comment
Erika
Erika on 16 Jan 2022
Hello, Tina!
Thank you so much for taking your time to answer my question, it really means a lot.
After much realization, it turns out that my code is correct all along. I figured that the display part of the code should've been inside the loop and not outside.
Again, thank you so much!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!