Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typo and format fixing #46

Merged
merged 14 commits into from
Dec 4, 2023
2 changes: 1 addition & 1 deletion code/02-arrays.m
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
M(5, 1:8)

% ## The key-word `end`
M(2,end)
M(2, end)
M(5:end,3)

M(5, 1:end)
Expand Down
24 changes: 24 additions & 0 deletions code/06-cond.m
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,30 @@

% ! Challenge:
% ## Changing behaviour based on patient data
% Load patient data
patient_data = readmatrix('data/base/inflammation-01.csv');

% Compute global statistics
g_mean = mean(patient_data(:));
g_max = max(patient_data(:));
g_min = min(patient_data(:));

patient_number = 8;

% Compute patient statistics
p_mean = mean(patient_data(patient_number,:));
p_max = max(patient_data(patient_number,:));
p_min = min(patient_data(patient_number,:));

% Compare patient vs global
disp('Patient:')
disp(patient_number)
disp('High mean?')
disp(p_mean > g_mean)
disp('Highest max?')
disp(p_max == g_max)
disp('Lowest min?')
disp(p_min == g_min)
% !! Solution:
% Load patient data
patient_data = readmatrix('data/base/inflammation-01.csv');
Expand Down
6 changes: 3 additions & 3 deletions code/07-func.m
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,9 @@ function plot_daily_average(data_file,plot_name)
% ## Plotting patient vs mean
% !! Solution:
function patient_vs_mean(per_day_mean,patient_data,patient_reference)
% PATIENT_VS_MEAN Plots the global mean and patient inflamation on top of each other.
% PATIENT_VS_MEAN Plots the global mean and patient inflammation on top of each other.
% per_day_mean should be a vector with the global mean.
% pataient_data should be a vector with only the patient data.
% patient_data should be a vector with only the patient data.
% patient_reference will be used to identify the patient on the plot.
%
% Sample usage:
Expand All @@ -152,7 +152,7 @@ function patient_vs_mean(per_day_mean,patient_data,patient_reference)

%Overlap patient data
hold on
plot(pataient_data,DisplayName=patient_reference)
plot(patient_data,DisplayName=patient_reference)
hold off

% Save plot
Expand Down
55 changes: 31 additions & 24 deletions code/08-loops.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
for letter = 1:4
disp(word(letter))
end
for variable = collection
# Do things with variable
end
%LOOP_DEMO Demo script to explain loops

word = 'tin';
Expand Down Expand Up @@ -98,14 +101,14 @@


% ## Analyzing patient data from multiple files
files = dir('data/inflammation-*.csv')
files = dir('data/base/inflammation-*.csv')
filename = files(1).name;
disp(filename)
mod_date = files(3).date;
disp(mod_date)
%PLOT_ALL Developing code to automate inflammation analysis

files = dir('data/inflammation-*.csv');
files = dir('data/base/inflammation-*.csv');

for i = 1:length(files)
file_name = files(i).name;
Expand All @@ -122,7 +125,7 @@

%PLOT_ALL Developing code to automate inflammation analysis

files = dir('data/inflammation-*.csv');
files = dir('data/base/inflammation-*.csv');

for i = 1:length(files)
file_name = files(i).name;
Expand All @@ -131,7 +134,7 @@
img_name = replace(file_name, '.csv', '.png');

% Generate path to data file and image file
file_name = fullfile('data', file_name);
file_name = fullfile('data', 'base', file_name);
img_name = fullfile('results',img_name);

disp(file_name)
Expand All @@ -140,7 +143,7 @@
%PLOT_ALL Print statistics for all patients.
% Save plots of statistics to disk.

files = dir('data/inflammation-*.csv');
files = dir('data/base/inflammation-*.csv');

% Process each file in turn
for i = 1:length(files)
Expand All @@ -150,33 +153,37 @@
img_name = replace(file_name, '.csv', '.png');

% Generate path to data file and image file
file_name = fullfile('data', file_name);
file_name = fullfile('data', 'base', file_name);
img_name = fullfile('results', img_name);

patient_data = readmatrix(file_name);
plot_daily_average(file_name, img_name);

% Create figures
figure(visible='off')
end
plot_all

% ! Challenge:
% ## Investigating patients with a high mean
% !! Solution:
% PLOT_HIGH_MEAN_PATIENTS Saves plots of patients with mean inflammation higher than the global mean inflammation.

tlo = tiledlayout(1,3);
xlabel(tlo,'Day of trial')
ylabel(tlo,'Inflammation')
patient_data = readmatrix('data/base/inflammation-01.csv');

nexttile
plot(mean(patient_data, 1))
title('Average')
per_day_mean = mean(patient_data);
global_mean = mean(patient_data(:));

nexttile
plot(max(patient_data, [], 1))
title('Max')
number_of_patients = size(patient_data,1);

nexttile
plot(min(patient_data, [], 1))
title('Min')
for patient_id = 1:number_of_patients

patient_mean = mean(patient_data(patient_id,:));

if(patient_mean > global_mean)
patient_reference = "Patient " + string(patient_id)
patient_vs_mean(per_day_mean, patient_data(patient_id,:), patient_reference)
end

print(img_name, '-dpng')
close()
end
plot_all




22 changes: 11 additions & 11 deletions episodes/01-intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ x =
55
```

Notice that matlab responded by printing an output confirming that the variable has the desired value,
Notice that MATLAB responded by printing an output confirming that the variable has the desired value,
and also that the variable appeared in the workspace.

A variable is just a name for a piece of data or *value*.
Expand Down Expand Up @@ -162,7 +162,7 @@ There are only two options (yes or no, true or false, 0 or 1),
and so it is "cheaper" for the computer to save space only for those two options.

The "type" of this data is not the same as the "type" of data that represents a number.
It comes from a logical comparison, and so matlab identifies it as such.
It comes from a logical comparison, and so MATLAB identifies it as such.

You can also see that in the workspace these variables have a tick next to them, instead of the squares we had seen.
There are actually other symbols that appear there, relating to the different types of information we can save in
Expand Down Expand Up @@ -265,11 +265,11 @@ We often asks questions or characterise things in negative.
and "I didn't shoot no deputy" are just some examples.

Naturally, we may want to do so in programming too.
In matlab the negative is represented with `~`.
In MATLAB the negative is represented with `~`.
For example, we can check if the speed is indeed not faster than the limit with `~(speed > 70)`,
which matlab reads as "not speed greater than 70".
which MATLAB reads as "not speed greater than 70".

Can you express these questions in matlab code?
Can you express these questions in MATLAB code?

- Is 1 + 2 + 3 + 4 not smaller than 10?
- Is 5 to the power of 3 different from 125?
Expand All @@ -283,7 +283,7 @@ We can ask the first two question in positive, encapsulate it in brackets, and t
- `~(1 + 2 + 3 + 4 < 10)`
- `~(5^3 == 125)`

Asking if two things are different is so common, that matlab has a special symbol for it.
Asking if two things are different is so common, that MATLAB has a special symbol for it.
So the second question, we could have asked instead with

- `5^3 ~= 125`
Expand All @@ -292,7 +292,7 @@ We can ask if x+y is greater or equal to x/y with:

- `x+y > x/y || x+y == x/y`

There is actually again a shortcut for this, matlab understands `>=` as "greater or equal to",
There is actually again a shortcut for this, MATLAB understands `>=` as "greater or equal to",
and of cours for smaller or equal too it understands `<=`.
So the same condition could be written as:

Expand All @@ -313,7 +313,7 @@ Remembering to add the brackets, we get:
### Arrays

You may notice that all of the variable types start with a `1x1`.
This is because matlab thinks in terms of *groups* of variables called arrays, or matrices.
This is because MATLAB thinks in terms of *groups* of variables called arrays, or matrices.

We can create an array using square brackets and separating each value with a comma:
```matlab
Expand Down Expand Up @@ -365,7 +365,7 @@ C =

Something to bear in mind, however, is that all values in an array **must be of the same type**.

We mentioned before that matlab is actually more used to working with arrays than individual variables.
We mentioned before that MATLAB is actually more used to working with arrays than individual variables.
Well, if it is so used to working with arrays, can we do operations with them?

The answer is yes! In fact, this is what makes MATLAB a particularly interesting programming language.
Expand All @@ -382,7 +382,7 @@ ans =
1 1
```

Matlab then compared each element of B and asked "is this element greater than 3?".
MATLAB then compared each element of B and asked "is this element greater than 3?".
The result is another array, of the same size and dimensions as B, with the answers.

We can also do sums, multiplications, and pretty much anything we want with an array, but we need to be careful with what we do.
Expand Down Expand Up @@ -455,7 +455,7 @@ If we want to delete a variable we can do so by typing `clear` and the name of t
>> clear alive_on_day_3
```
You might be able to see it disappear from the workspace.
If you now try to use alive_on_day_3, matlab will give an error.
If you now try to use alive_on_day_3, MATLAB will give an error.

We can also delete **all** of our variables with the command `clear`, without any variable names following it.
Be careful though, there's no way back!
Expand Down
26 changes: 13 additions & 13 deletions episodes/02-arrays.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ exercises: 30

## Initializing an Array

We just talked about how matlab *thinks* in arrays, and declared some very simple arrays using square brackets.
We just talked about how MATLAB *thinks* in arrays, and declared some very simple arrays using square brackets.
In some cases, we will want to create space to save data, but not save anything just yet.
One way of doing so is with `zeros`.
The function [zeros](https://uk.mathworks.com/help/matlab/ref/zeros.html)
Expand All @@ -34,7 +34,7 @@ Z =
0 0 0 0 0
```
creates a matrix of 3 rows and 5 columns, filled with zeros.
If we had only passed one dimension, matlab assumes you want a square matrix, so
If we had only passed one dimension, MATLAB assumes you want a square matrix, so
```matlab
>> Z = zeros(3)
```
Expand All @@ -44,8 +44,8 @@ Z =
0 0 0
0 0 0
```
yields a 3x3 array.
If we want a single row and 5 columns, we need to remember that matlab reads `rows`x`columns`, so
yields a 3×3 array.
If we want a single row and 5 columns, we need to remember that MATLAB reads `rows`×`columns`, so
```matlab
>> Z = zeros(1x5)
```
Expand Down Expand Up @@ -78,7 +78,7 @@ So for example,
```matlab
>> Fives = ones(3,6)*5;
```
Produces a 3x6 matrix full of fives.
Produces a 3×6 matrix full of fives.


The [`magic` function](https://uk.mathworks.com/help/matlab/ref/magic.html) works in a similar way,
Expand All @@ -105,7 +105,7 @@ Array **indexing**, is the method by which we can select one or more different e
A solid understanding of array indexing will be essential to working with arrays.
Lets start with selecting one element.

First, we will create an 8x8 "magic" matrix:
First, we will create an 8×8 "magic" matrix:

```matlab
>> M = magic(8)
Expand Down Expand Up @@ -198,7 +198,7 @@ The rows and columns we just selected could have been specified as ranges.
So if we want the rows from 4 to 6 and columns from 5 to 7,
we can specify the ranges as `4:6` and `5:7`.
On top of being a much quicker and neater way to get the rows and columns,
matlab knows that the range will produce an array, so we do not even need the square brackets anymore.
MATLAB knows that the range will produce an array, so we do not even need the square brackets anymore.
So the command above becomes:
```matlab
>> M(4:6, 5:7)
Expand Down Expand Up @@ -258,17 +258,17 @@ However, we need to know that there are 8 columns, which is not very robust.

When indexing the elements of an array, the key word `end` can be used to get the last index available.

For example, `M(2,end)` returns the last element of the second row:
For example, `M(2, end)` returns the last element of the second row:
```matlab
>> M(2,end)
>> M(2, end)
```
```output
ans =
16
```

We can also use it in combination with the `:` operator.
For example, `M(5:end,3)` returns the elements of column 3 from row 5 until the end:
For example, `M(5:end, 3)` returns the elements of column 3 from row 5 until the end:
```matlab
>> M(5:end,3)
```
Expand Down Expand Up @@ -296,7 +296,7 @@ This is much better, now this works for any size of matrix, and we don't need to

## Using `:` as an index

Getting a whole row or column is such a common operation, that matlab has a shortcut:
Getting a whole row or column is such a common operation, that MATLAB has a shortcut:
Using `:` alone is equivalent to `1:end`!

For example, We can then get the whole fifth row with:
Expand Down Expand Up @@ -463,9 +463,9 @@ last three characters: gen
They all produce a square matrix if only one argument is given,
but you can specify the dimensions you want separated by a comma, as in `zeros(rows,columns)`.
- To select data points we use round brackets and provide the row and column indices of the elements we want.
They can be just numbers or arrays of numbers. E.g. `M(5,[3,4,5])`"
They can be just numbers or arrays of numbers. E.g. `M(5, [3,4,5])`"
- We can use the colon operator `:` to generate ordered arrays as `start:end` or `start:increment:end`.
- We can use the keyword `end` to get the index of the last element.
- To obtain the index of the final element, the keyword `end` can be employed.
- The colon operator by itself `:` selects all the elements.

::::::::::::::::::::::::::::::::::::::::::::::::::
Expand Down
Loading