Programming Basics for Logistics Algorithms: Lecture 4
1
Course Overview
2
Course Overview
3
Flashback: Lecture 3
4
2D Arrays: Matrices
5
>> A = zeros (5,5)
A =
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
>> B = ones (3,4)
B =
1 1 1 1
1 1 1 1
1 1 1 1
>> C = rand (2,3)
C =
0.5504 0.0791 0.8982
0.5963 0.5766 0.4633
>> D = randi (10, 2,3)
D =
10 2 9
7 4 6
range
Matrix Indexing
6
b(1)
b(2)
b(3)
b(4)
b(1,1)
b(1,2)
b(1,1)
b(2,1)
b(1,2)
b(2,2)
Adding Elements to Matrix
7
It is not possible to add just 1 element to the matrix!
But we can add entire rows or columns:
EXAMPLE:
A = 3 3 4 A(:,4)= [1, 1, 1, 1]’ 3 13 4 1
5 3 2 5 3 2 1
2 3 2 2 3 2 1
1 3 1 1 3 1 1
A = 3 3 4 A(:,5)= [1,1,1,1]’ 3 13 4 0 1
5 3 2 5 3 2 0 1
2 3 2 2 3 2 0 1
1 3 1 1 3 1 0 1
Size, Length and Numel
A =
10 2 9
7 4 6
>> length(A) =
ans =
3
>> numel(A) =
ans =
6
8
>> size(A)
ans =
2 3
Operators
Matrix Operations
9
●
●
○
●
=
Element-wise functions
Element-wise operations
10
●
○
●
●
○
○
Pay attention to which mode you mean to use!
Graphs
11
ORD
PVD
MIA
DFW
SFO
LAX
LGA
HNL
849
802
1387
1743
1843
1099
1120
1233
337
2555
142
Implementing a Graph
12
X
U
V
W
Z
Y
a
c
b
e
d
f
g
h
i
j
13
14
Next: Plotting
15
Questions?
Image Processing using Matlab
16
Plotting
17
» plot(y);
» plot(x,y);
�Example: plotting a point
x = 11;
y = 18;
plot(x,y,'r*')
Plotting
18
x = 11;
y = 18;
plot(x,y,'r*')
xlabel('Time')
ylabel('Temperature')
title('Time and Temp')
Plotting
19
x = 11;
y = 18;
plot(x,y,'r*')
xlabel('Time')
ylabel('Temperature')
title('Time and Temp')
axis([x-2 x+2 y-5 y+5])
Plotting a line
20
» x = 1:10
» y = 2*x
» plot (x,y)
MATLAB makes visualizing data fun and easy!
Plot Options
21
More plotting options
22
Colors Plot symbols Line types
More Plot Controls
23
Axis control:
axis - Control axis scaling and appearance
zoom - Zoom in and out on a 2-D plot
grid - Grid lines
box - Axis box
rbbox - Rubberband box
hold - Hold current graph (hold on, hold off options)
axes - Create axes in arbitrary positions
subplot - Create axes in tiled positions
Graph annotation:
title - Graph title
xlabel - X-axis label
ylabel - Y-axis label
texlabel - Produces the TeX format from a character string
text - Text annotation
gtext - Place text with mouse
Playing with the Plot
24
to select lines and delete or change properties
to zoom in/out
to slide the plot around
to see all plot tools at once
Copy/Paste Figures
25
Saving Figures
26
.fig preserves all information
.bmp uncompressed image
.eps high-quality scaleable format
.pdf compressed image
3D Line Plots
27
» t=0:0.001:4*pi;
» x = sin(t);
» y = cos(t);
» plot3(x,y,t,'k','LineWidth',2);
» xlim, ylim, zlim
Surface Plots
28
Visualizing matrices
29
» mat = reshape (1:10000,100,100);
» imagesc (mat);
» colorbar
» caxis ([3000 7000])
surf
30
» x = -pi:0.1:pi;
» y = -pi:0.1:pi;
» [X,Y] = meshgrid (x,y);
To get function values, evaluate the matrices
» Z = sin(X) .*cos(Y);
» surf (X,Y,Z)
» surf(X,Y,Z);
)
rane
*Try typing surf(memb
surf Options
31
faceted
flat
See help surf for more options
There are three types of surface shading
» shading facetad
» shading flat
» shading
» colormap (grey)
35
35
interp
flat
contour
32
» contour (X,Y,Z, ‘LineWidth’, 2)
» hold on
» mesh (X,Y,Z)
Specialized Plotting Functions
33
» polar (0:0.01:2*pi, cos ((0:0.01:2*pi)*2));
» bar (1:10, rand(1,10));
» [X,Y] = meshgrid (1:10,1:10);
» quiver(X,Y, rand(10),rand(10));
» stairs(1:10, rand(1,10));
» fill ([0 1 0.5], [0 0 1], ‘r’);
Visualizing Graphs
graph objects represent undirected graphs, which have direction-less edges connecting the nodes
>>clear ;
A = [
0 1 0 1;
1 0 1 0;
0 1 0 1;
1 0 1 0
];
figure;
G = graph (A);
plot (G);
Graph created using adjacency matrix
34
Visualizing Graphs
digraph objects represent directed graphs, which have directional edges connecting the nodes
clear ;
A = [
0 1 0 1;
1 0 1 0;
0 1 0 1;
1 0 1 0
];
figure;
G = digraph (A);
plot (G);
35
Visualizing Graphs
Defining edge list - another way of creating graphs
E_list = [
1, 2;
1, 3;
2, 3
]; % edge list
E_weights = [1 1 2];
G = graph(E_list(:,1), E_list(:,2), E_weights);
plot (G);
36
Visualizing Graphs
Defining edge list - another way of creating graphs
E_list = [
1, 2;
1, 3;
2, 3
]; % edge list
E_weights = [1 1 2];
G = graph(E_list(:,1), E_list(:,2), E_weights);
% to add labels to the nodes:
G.Nodes.Name = { 'Olle ' 'Sara ' 'Karl ' }';
% to mark weights on the graph:
plot (G );
37
Visualizing Graphs
Defining edge list - another way of creating graphs
E_list = [
1, 2;
1, 3;
2, 3
]; % edge list
E_weights = [1 1 2];
G = graph(E_list(:,1), E_list(:,2), E_weights);
% to add labels to the nodes:
G.Nodes.Name = { 'Olle ' 'Sara ' 'Karl ' }';
% OR: to mark weights on the graph:
plot (G , 'EdgeLabel', G.Edges.Weight);
38
Questionnaire
39
Next: Data Management
40
Questions?
41
File Operations
42
Read from the file
load filename
43
File Operations
save filename matrixname -ascii
44
File Operations
save filename matrixname -ascii -append
45
File Operations
type filename
Example:
» type testfile.dat
2 4 6 1 0 0 4
2 3 4 1 2 3 4
1 1 2 3 4 5 1
1 2 5 6 2 3 5
46
File Operations
Example: delete the first column from the matrix in file testfile.dat
» type testfile.dat
2 4 6 1 0 0 4
2 3 4 1 2 3 4
1 1 2 3 4 5 1
1 2 5 6 2 3 5
» load testfile.dat
» testfile(:,1) = []; % to delete first column in the matrix
» save testfile.dat testfile -ascii
» type testfile.dat
4.0000000e+00 6.0000000e+00 1.0000000e+00 0.0000000e+00 0.0000000e+00 4.0000000e+00
3.0000000e+00 4.0000000e+00 1.0000000e+00 2.0000000e+00 3.0000000e+00 4.0000000e+00
1.0000000e+00 2.0000000e+00 3.0000000e+00 4.0000000e+00 5.0000000e+00 1.0000000e+00
2.0000000e+00 5.0000000e+00 6.0000000e+00 2.0000000e+00 3.0000000e+00 5.0000000e+00
47
File Operations
Example: delete the first column from the matrix in file testfile.dat
» type testfile.dat
4.0000000e+00 6.0000000e+00 1.0000000e+00 0.0000000e+00 0.0000000e+00 4.0000000e+00
3.0000000e+00 4.0000000e+00 1.0000000e+00 2.0000000e+00 3.0000000e+00 4.0000000e+00
1.0000000e+00 2.0000000e+00 3.0000000e+00 4.0000000e+00 5.0000000e+00 1.0000000e+00
2.0000000e+00 5.0000000e+00 6.0000000e+00 2.0000000e+00 3.0000000e+00 5.0000000e+00
» testfile = int8(testfile)
testfile =
4×6 int8 matrix
4 6 1 0 0 4
3 4 1 2 3 4
1 2 3 4 5 1
2 5 6 2 3 5
>> save testfile.dat testfile -ascii
Warning: Attempt to write an unsupported data type to an ASCII file.
Variable 'testfile' not written to file.
48
Advanced File Operations
fopen, fclose, fscanf, textscan, fgetln, xlsread
fread, fwrite, fseek, and frewind, and more...
Some examples:
https://www.mathworks.com/help/matlab/learn_matlab/data-analysis.html
https://www.mathworks.com/help/matlab/examples.html?category=data-import-and-analysis
https://www.mathworks.com/help/matlab/import_export/add_spreadsheet_data_arrays_variables.html
49
Advanced File Operations
.txt, .dat, or .csv for delimited text files
.xls, .xlsb, .xlsm, .xlsx, .xltm, .xltx, or .ods for spreadsheet files
50
Advanced File Operations
51
Cell Arrays
Example:
» cellrowvec = {23, 'a', 1:2:9, 'hello'}
cellrowvec =
[23] 'a' [1x5 double] 'hello'
52
Cell Arrays: Indexing
Example:
» cellrowvec = {23, 'a', 1:2:9, 'hello'}
cellrowvec =
[23] 'a' [1x5 double] 'hello'
» cellrowvec(3)
ans =
1×1 cell array
{1×5 double}
» cellrowvec{3}
ans =
1 3 5 7 9
Use curly braces {}
to access values stored in cell array!
53
Table Arrays
Example:
» T = table(Age,Height,Weight,'RowNames', LastName)
» disp (T.Age)
» meanHeight = mean (T.Height)
54
Advanced File Operations
Example: extract data subset from a data file
% clear Workspace
clear; clear all
% load flight delay data into the array variable delay_data
delay_data = readtable('alldelays.csv');
55
Advanced File Operations
EUROCONTROL Performance Review Unit (PRU): ATFM arrival flight delays
56
Advanced File Operations
EUROCONTROL Performance Review Unit (PRU): ATFM arrival flight delays
Data covers:
Years 2016-2024 (not much data for the pandemics years 2020-2021)
All European airports
57
Advanced File Operations
Example: extract data subset from a data file
% clear Workspace
clear; clear all
% load flight delay data into the array variable delay_data
delay_data = readtable('all delay.csv');
% extract delay data for the particular airport of Munich
munich_data = delay_data(strcmp(delay_data.APT_ICAO, 'EDDM'), :);
% save the delay data for Munich airport in a separate data file
writetable(flight_data, 'munich.csv');
58
Advanced File Operations
EUROCONTROL Performance Review Unit (PRU): ATFM arrival flight delays
Query examples:
59
Advanced File Operations
Example: analyse the delays in Munich airport
% Clean the data to leave only the rows containing information about the delays
num_lines = size(flight_data,1)
count = 0;
for i=1:(num_lines-1)
if (munich_data.DLY_APT_ARR_1{i}>0)
count = count +1;
delay_data_munich(count,:) = flight_data(i,:);
end
end
writetable(delay_data1,'munich_delays.csv')
THANK YOU!
60
Questions?