From 678690780ab8c1ac0a654c355f349f0a36617190 Mon Sep 17 00:00:00 2001 From: EAtbas Date: Tue, 28 Nov 2023 16:06:14 +0000 Subject: [PATCH 01/14] chore: Fix Index.md --- index.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/index.md b/index.md index 5f355527..f84f9752 100644 --- a/index.md +++ b/index.md @@ -8,10 +8,10 @@ introduction to [MATLAB](https://en.wikipedia.org/wiki/MATLAB) is built around a Our real goal isn't to teach you MATLAB, but to teach you the basic concepts that all programming depends on. We use MATLAB in our lessons because: -1. we have to use *something* for examples; -2. it's well-documented; -3. it has a large (and growing) user base among scientists in academia and industry; and -4. it has a large library of packages available for performing diverse tasks. +1. We have to use *something* for examples; +2. It's well-documented; +3. The user base among scientists in academia and industry is large and continues to grow; and +4. It has a large library of packages available for performing diverse tasks. But the two most important things are to use whatever language your colleagues are using, so that you can share your work with them easily, and to use that From 0dc4dfee40e5825f81726372c463f494b1c04b1d Mon Sep 17 00:00:00 2001 From: EAtbas Date: Tue, 28 Nov 2023 16:10:12 +0000 Subject: [PATCH 02/14] chore: Fix Intro.md --- episodes/01-intro.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/episodes/01-intro.md b/episodes/01-intro.md index 4010ce71..90bacbb2 100644 --- a/episodes/01-intro.md +++ b/episodes/01-intro.md @@ -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. From 3a0f05e118c774e4ff42f0f57ad8f5358adeb3d8 Mon Sep 17 00:00:00 2001 From: EAtbas Date: Tue, 28 Nov 2023 16:17:35 +0000 Subject: [PATCH 03/14] chore: Fix Arrays.md --- episodes/02-arrays.md | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/episodes/02-arrays.md b/episodes/02-arrays.md index faaf7567..c187edff 100644 --- a/episodes/02-arrays.md +++ b/episodes/02-arrays.md @@ -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) @@ -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) ``` @@ -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, @@ -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) @@ -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) @@ -258,9 +258,9 @@ 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 = @@ -268,7 +268,7 @@ ans = ``` 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) ``` @@ -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. :::::::::::::::::::::::::::::::::::::::::::::::::: From 7ab5730b372b11d53581f41de42d9585f5008912 Mon Sep 17 00:00:00 2001 From: EAtbas Date: Tue, 28 Nov 2023 16:22:48 +0000 Subject: [PATCH 04/14] chore: Fix loading_data.md --- episodes/03-loading_data.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/episodes/03-loading_data.md b/episodes/03-loading_data.md index da4035c7..0cb3a635 100644 --- a/episodes/03-loading_data.md +++ b/episodes/03-loading_data.md @@ -260,7 +260,7 @@ we want to select all the rows for a given column: >> day_9 = patient_data(:,9); ``` The result is now not a row of 40 elements, but a column with 60 items. -However, matlab is smart enough to figure out what to do with enquieries just like the ones we did before. +However, MATLAB is smart enough to figure out what to do with enquieries just like the ones we did before. ```matlab >> mean_d9 = mean(day_9) >> max_d9 = max(day_9) @@ -317,7 +317,7 @@ Would you be happy to do it if you had 1000 days worth of data? #### **Whole array analysis** The analysis we've done until now would be very tedious to repeat for each patient or day. -Luckily, we've learnt that matlab is used to thinking in terms of arrays. +Luckily, we've learnt that MATLAB is used to thinking in terms of arrays. Surely it must be possible to get the mean of each patient or each day in one go. It is definitely tempting to simply call the mean on the array, so let's try it: ```matlab @@ -334,7 +334,7 @@ The other statistics behave in the same way, so we can more appropriately label >> per_day_std = std(patient_data); ``` -You'll notice that each of the above variables is a `1x40` array. +You'll notice that each of the above variables is a `1×40` array. Now that we have the information for each day in an array, we can take advantage of Matlab's capacity to do array operations. @@ -420,7 +420,7 @@ If we want patient averages, we want the columns to be averaged, that is, dimens >> per_patient_mean = mean(patient_data,2); ``` -As expected, the result is a `60x1` vector, with the mean for each patient. +As expected, the result is a `60×1` vector, with the mean for each patient. Unfortunately, `max`, `min` and `std` do not behave quite in the same way. If you explore their documentation, you'll see that we need to add another argument, @@ -431,7 +431,7 @@ so that the commands become: >> per_patient_std = std(patient_data,[],2); ``` -All of the above return a `60x1` vector. +All of the above return a `60×1` vector. ::::::::::::::::::::::::::::::::::::::: challenge @@ -441,7 +441,7 @@ Can you find the patients that got the highest inflamation? ::::::::::::::: solution -Using the power matlab has to compare arrays, +Using the power MATLAB has to compare arrays, we can check which patients have a `max` equal to the `global_max`. If we wrap this check in the find function, we get the row numbers: ```matlab From 822c7302eca5200ef4f5cf7382c7d84cb83cb472 Mon Sep 17 00:00:00 2001 From: EAtbas Date: Tue, 28 Nov 2023 16:26:04 +0000 Subject: [PATCH 05/14] chore: Fix plotting.md --- episodes/04-plotting.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/episodes/04-plotting.md b/episodes/04-plotting.md index 0c02400f..69056c50 100644 --- a/episodes/04-plotting.md +++ b/episodes/04-plotting.md @@ -22,7 +22,7 @@ exercises: 10 ## Plotting The mathematician Richard Hamming once said, "The purpose of computing is insight, not numbers," and the best -way to develop insight is often to visualize data. Visualization +way to develop insight is often to visualise data. Visualisation deserves an entire lecture (or course) of its own, but we can explore a few features of MATLAB here. @@ -100,7 +100,7 @@ For example, we might want to contrast the mean values accross patients with the inflammation of a single patient. If we are displaying more than one line, it is important to add a legend. We can specify the legend names by adding `,DisplayName="legend name here"` -inside the plot function. We then need to activate the legend by running `legend` +inside the plot function. We then need to activate the legend by running `legend`. So, to plot the mean values we first do: ```matlab >> plot(per_day_mean,DisplayName="Mean") @@ -123,7 +123,7 @@ Then, we can use the instruction `hold on` to add a plot for patient_5. So this patient seems fairly average. -Remember to tell matlab you are done by adding `hold off` when you have finished adding lines to the figure! +Remember to tell MATLAB you are done by adding `hold off` when you have finished adding lines to the figure! ::::::::::::::::::::::::::::::::::::::: challenge From 930755442efdd8db633690073729002ae3c4b375 Mon Sep 17 00:00:00 2001 From: EAtbas Date: Tue, 28 Nov 2023 16:29:40 +0000 Subject: [PATCH 06/14] chore: Fix scripts.md --- episodes/05-scripts.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/episodes/05-scripts.md b/episodes/05-scripts.md index 0982a52b..eb244432 100644 --- a/episodes/05-scripts.md +++ b/episodes/05-scripts.md @@ -97,7 +97,7 @@ disp(p_min == g_min) ## Comments -You might have noticed that we described what we want our code to do in lines starting withg the percent sign: `%`. +You might have noticed that we described what we want our code to do in lines starting with the percent sign: `%`. This is another plus of writing scripts: you can comment your code to make it easier to understand when you come back to it after a while. @@ -297,7 +297,7 @@ title('Min') The script now allows us to create all 3 plots with a single command: `plot_daily_average`. -We can ask matlab to save the image too using the `print` command. +We can ask MATLAB to save the image too using the `print` command. In order to maintain an organised project we'll save the images in the `results` directory: From 08d0f989abaaef32f6ce41972d843e1b9a986ac9 Mon Sep 17 00:00:00 2001 From: EAtbas Date: Tue, 28 Nov 2023 16:37:20 +0000 Subject: [PATCH 07/14] chore: Fix func.md --- episodes/07-func.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/episodes/07-func.md b/episodes/07-func.md index 695f1aec..c1a24bfc 100644 --- a/episodes/07-func.md +++ b/episodes/07-func.md @@ -9,7 +9,7 @@ exercises: 20 - Learn how to write a function - Define a function that takes arguments. - Compare and contrast MATLAB function files with MATLAB scripts. -- Recognize why we should divide programs into small, single-purpose functions. +- Recognise why we should divide programs into small, single-purpose functions. :::::::::::::::::::::::::::::::::::::::::::::::::: @@ -220,9 +220,9 @@ function patient_analysis(patient_number) end ``` -Congratulations! You've now created a Matlab function from a Matlab script! +Congratulations! You've now created a MATLAB function from a MATLAB script! -You may have noticed that the code inside the function is indented. Matlab does not need this, but it makes it much more +You may have noticed that the code inside the function is indented. MATLAB does not need this, but it makes it much more readable! Lets clear our workspace and run our function in the command line: @@ -251,7 +251,7 @@ If we want to save them, we need to pass them as outputs. Lets say, for example, that we want to save the mean of each patient. In our `patient_analysis.m` we already compute the value and save it in `p_mean`, -but we need to tell matlab that we want the function to return it. +but we need to tell MATLAB that we want the function to return it. To do that we modify the function definition like this: ```matlab @@ -446,7 +446,7 @@ end of the function defined in the file. - Define functions using the `function` keyword to start the definition, and close the definition with the keyword `end`. - Functions have an independent workspace. Access variables from your workspace inside a function by passing them as inputs. Access variables from the function returning them as outputs. -- The header of a dunction with inputs an outputs has the form: +- The header of a function with inputs an outputs has the form: ```function [output_1,output_2,...] = function_name(input_1,input_2,...)``` From 659f55abdabda8710e3b3541008ff2c026d4a75d Mon Sep 17 00:00:00 2001 From: EAtbas Date: Tue, 28 Nov 2023 16:45:38 +0000 Subject: [PATCH 08/14] chore: Fix loops.md --- episodes/08-loops.md | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/episodes/08-loops.md b/episodes/08-loops.md index 0e511c79..8231023e 100644 --- a/episodes/08-loops.md +++ b/episodes/08-loops.md @@ -21,10 +21,10 @@ exercises: 10 Recall that we have twelve datasets in total. -We're going to need a better way to analyze them all than typing out commands for each one, +We're going to need a better way to analyse them all than typing out commands for each one, because we'll find ourselves writing a lot of duplicated code. Code that is repeated in two or more places will eventually be wrong in at least one as our project develops over time. -Also, if we make changes in the way we analyze our datasets, +Also, if we make changes in the way we analyse our datasets, we have to introduce that change in every copy of our code. To avoid all of this repetition, we have to teach MATLAB to repeat our commands, and to do *that*, we have to learn how to write *loops*. @@ -77,7 +77,6 @@ disp(word(4)) ``` error: A(I): index out of bounds; value 4 out of bound 3 ``` -{: .error} There's a better approach: @@ -99,7 +98,7 @@ d ``` This improved version uses a [for loop]({{ page.root }}/reference.html#for-loop) to -repeat an operation---in this case, printing to the screen---once for +repeat an operation --- in this case, printing to the screen --- once for each element in an array. The general form of a for loop is: @@ -109,7 +108,6 @@ for variable = collection do things with variable end ``` -{: .source} The for loop executes the commands in the [loop body]({{ page.root }}/reference.html#loop-body) @@ -152,7 +150,7 @@ it's not the best way to write our loop: * We might update `word` and forget to modify the loop to reflect that change. -* We might make a mistake while counting the number of letters in +* We could make a mistake while counting the number of letters in `word`. Fortunately, From 03fef332dc859e957d7b8bd1ad838914b6a1d294 Mon Sep 17 00:00:00 2001 From: EAtbas Date: Tue, 28 Nov 2023 16:45:49 +0000 Subject: [PATCH 09/14] chore: Fox reference.md --- learners/reference.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/learners/reference.md b/learners/reference.md index 698d168a..f7b7a598 100644 --- a/learners/reference.md +++ b/learners/reference.md @@ -2,16 +2,16 @@ title: 'Learning resources' --- -[Matlab online](https://mathworks.com/products/matlab-online.html) lets you use the software for free, which can be useful when you do not have access to the university licences. +[MATLAB online](https://mathworks.com/products/matlab-online.html) lets you use the software for free, which can be useful when you do not have access to the university licences. -The [totorials section](https://mathworks.com/support/learn-with-matlab-tutorials.html) +The [tutorials section](https://mathworks.com/support/learn-with-matlab-tutorials.html) on MATLAB's site also lists useful video tutorials and examples to work through. [MATLAB Academy](https://matlabacademy.mathworks.com/) provides a lot of self-taught material and free courses. In particular, the [Onramp](https://matlabacademy.mathworks.com/details/matlab-onramp/gettingstarted) program covers material similar to this course. -[Matlab cody](https://mathworks.com/matlabcentral/cody) can be a good way to keep practicing and find interesting challenges to learn how to code with MATLAB. +[MATLAB cody](https://mathworks.com/matlabcentral/cody) can be a good way to keep practicing and find interesting challenges to learn how to code with MATLAB. ## Glossary From d5fefff0d355960434ed94f0b7864e98f16348d5 Mon Sep 17 00:00:00 2001 From: fherreazcue Date: Mon, 4 Dec 2023 10:07:11 +0000 Subject: [PATCH 10/14] MATLAB instead of matlab --- episodes/01-intro.md | 20 ++++++++++---------- episodes/02-arrays.md | 6 +++--- episodes/03-loading_data.md | 2 +- episodes/05-scripts.md | 2 +- episodes/07-func.md | 2 +- 5 files changed, 16 insertions(+), 16 deletions(-) diff --git a/episodes/01-intro.md b/episodes/01-intro.md index 90bacbb2..e52652c2 100644 --- a/episodes/01-intro.md +++ b/episodes/01-intro.md @@ -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*. @@ -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 @@ -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? @@ -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` @@ -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: @@ -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 @@ -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. @@ -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! diff --git a/episodes/02-arrays.md b/episodes/02-arrays.md index c187edff..2adf6183 100644 --- a/episodes/02-arrays.md +++ b/episodes/02-arrays.md @@ -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) ``` @@ -45,7 +45,7 @@ Z = 0 0 0 ``` yields a 3×3 array. -If we want a single row and 5 columns, we need to remember that matlab reads `rows`×`columns`, so +If we want a single row and 5 columns, we need to remember that MATLAB reads `rows`×`columns`, so ```matlab >> Z = zeros(1x5) ``` @@ -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: diff --git a/episodes/03-loading_data.md b/episodes/03-loading_data.md index 0cb3a635..21038835 100644 --- a/episodes/03-loading_data.md +++ b/episodes/03-loading_data.md @@ -52,7 +52,7 @@ A final step is to set the *current folder* in MATLAB to our project folder. Use the **Current Folder** window in the MATLAB GUI to browse to your project folder (the one now containing the 'data', 'results' and 'src' directories). -To verify the current directory in matlab we can run `pwd` (print working directory). +To verify the current directory in MATLAB we can run `pwd` (print working directory). ```matlab >> pwd ``` diff --git a/episodes/05-scripts.md b/episodes/05-scripts.md index eb244432..c27bfd82 100644 --- a/episodes/05-scripts.md +++ b/episodes/05-scripts.md @@ -41,7 +41,7 @@ edit src/patient_analysis.m ``` Matlab will create a file called `patient_analysis.m` in the `src` folder. -It is important that we let matlab know that we want it to find stuff in this folder. +It is important that we let MATLAB know that we want it to find stuff in this folder. To do this, right click on the folder icon in the file browser and select "Add to Path". ::::::::::::::::::::::::::::::::::::::::: callout diff --git a/episodes/07-func.md b/episodes/07-func.md index c1a24bfc..a225e0bc 100644 --- a/episodes/07-func.md +++ b/episodes/07-func.md @@ -183,7 +183,7 @@ We already have a `.m` file called `patient_analysis`, so lets begin by defining Open the `patient_analysis.m` file, if you don't already have it open. Instead of line 9, where `patient_number` is set, we want to provide that variable as an input. So lets remove that line, and right at the top of our script we'll add the function definition -telling matlab what our function is called and what inputs it needs. The function will take the variable `patient_number` +telling MATLAB what our function is called and what inputs it needs. The function will take the variable `patient_number` as input and since we removed the line that assigned a value to that variable, the input will decide which patient is analysed. From 0a0903233c2699a5312b938479e6b77a3fb99720 Mon Sep 17 00:00:00 2001 From: fherreazcue Date: Mon, 4 Dec 2023 10:14:17 +0000 Subject: [PATCH 11/14] fix code headers --- episodes/08-loops.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/episodes/08-loops.md b/episodes/08-loops.md index 8231023e..ea279854 100644 --- a/episodes/08-loops.md +++ b/episodes/08-loops.md @@ -74,7 +74,7 @@ disp(word(3)) disp(word(4)) ``` -``` +```output error: A(I): index out of bounds; value 4 out of bound 3 ``` @@ -103,9 +103,9 @@ each element in an array. The general form of a for loop is: -``` +```matlab for variable = collection - do things with variable + # Do things with variable end ``` From 43042268e7238a3047c4d6adb20abbc6c2c53796 Mon Sep 17 00:00:00 2001 From: fherreazcue Date: Mon, 4 Dec 2023 10:14:33 +0000 Subject: [PATCH 12/14] typos in plotting patient vs mean challenge --- episodes/07-func.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/episodes/07-func.md b/episodes/07-func.md index a225e0bc..017e52a8 100644 --- a/episodes/07-func.md +++ b/episodes/07-func.md @@ -386,10 +386,10 @@ Create a function called `patient_vs_mean` that generates a plot like this one: The function should have the following inputs: -- `per_day_mean` - A 1D array with the average inflamation per day already loaded +- `per_day_mean` - A 1D array with the average inflammation per day already loaded (you'll have to load the data and compute per_day_mean before calling the function). -- `pataient_data` - A 1D array with the data for the patient of interest only. +- `patient_data` - A 1D array with the data for the patient of interest only. - `patient_reference` - A string that will be used to identify the patient on the plot, and also as a file name (you should add the extension `png` in your function). @@ -402,9 +402,9 @@ Look back at the previous lessons if you need to! ```matlab 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: @@ -423,7 +423,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 From 093193dc3c17864de001af7cd5090aa6c01930dc Mon Sep 17 00:00:00 2001 From: fherreazcue Date: Mon, 4 Dec 2023 10:27:58 +0000 Subject: [PATCH 13/14] Fixed lsit in changing behaviour challenge and copied script for reference --- episodes/06-cond.md | 46 ++++++++++++++++++++++++++++++++++++--------- 1 file changed, 37 insertions(+), 9 deletions(-) diff --git a/episodes/06-cond.md b/episodes/06-cond.md index 48d4de1b..a3aab50f 100644 --- a/episodes/06-cond.md +++ b/episodes/06-cond.md @@ -315,20 +315,48 @@ to `true` and `false` does. ## Changing behaviour based on patient data -We'd like to improve our `patient_analysis` script from the previous lesson, specifically it's output. Currently the -script displays `0` or `1` to indicate whether or not the patient has a high mean, has a maximum equivalent to the -highest in the dataset, and has a minimum equivalent to the lowest in the dataset. Instead, we'd like the script to -print a line of descriptive text when each of these is true: +We'd like to improve our `patient_analysis` script from the previous lesson, specifically it's output. +Currently the script displays `0` or `1` to indicate whether or not the patient has a high mean, +has a maximum equivalent to the highest in the dataset, and has a minimum equivalent to the lowest in the dataset. +Instead, we'd like the script to print a line of descriptive text only when each of these is true: + 1. The mean inflammation for the patient is higher than the global mean. 2. The maximum inflammation for the patient is the same as the global maximum. 3. The minimum inflammation for the patient is the same as the global minimum. - -If none of the above is the case, then the script should print a line informing us that the patient's mean, maximum +4. If none of the above is the case, then the script should print a line informing us that the patient's mean, maximum and minimum inflammation are not remarkable. -Using the `patient_analysis` script from the previous lesson as a starting point, can you use conditional statements -to make a script that does this? There are several different ways to do this, so compare your finished script with -your neighbour and see if you did it the same way. +Using the `patient_analysis` script from the previous lesson as a starting point (shown below for reference), +can you use conditional statements to make a script that does this? + +```matlab +% 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) +``` + +There are several different ways to do this, so compare your finished script with your neighbour and see if you did it the same way. ::::::::::::::: solution ```matlab From 341178cae9e304bf2ad523a6bf71f251a787eef0 Mon Sep 17 00:00:00 2001 From: fherreazcue Date: Mon, 4 Dec 2023 10:39:17 +0000 Subject: [PATCH 14/14] extracted episodes code --- code/02-arrays.m | 2 +- code/06-cond.m | 24 +++++++++++++++++++++ code/07-func.m | 6 +++--- code/08-loops.m | 55 +++++++++++++++++++++++++++--------------------- 4 files changed, 59 insertions(+), 28 deletions(-) diff --git a/code/02-arrays.m b/code/02-arrays.m index 9434ccbf..e64bfa19 100644 --- a/code/02-arrays.m +++ b/code/02-arrays.m @@ -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) diff --git a/code/06-cond.m b/code/06-cond.m index f7fe5049..d2f17d8c 100644 --- a/code/06-cond.m +++ b/code/06-cond.m @@ -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'); diff --git a/code/07-func.m b/code/07-func.m index 149c7bf4..88795404 100644 --- a/code/07-func.m +++ b/code/07-func.m @@ -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: @@ -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 diff --git a/code/08-loops.m b/code/08-loops.m index bf1b3d7a..a53dcfea 100644 --- a/code/08-loops.m +++ b/code/08-loops.m @@ -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'; @@ -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; @@ -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; @@ -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) @@ -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) @@ -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 + +