Skip to content

Commit

Permalink
Finish variables and dimensions episode
Browse files Browse the repository at this point in the history
  • Loading branch information
christopher-wild committed Oct 15, 2024
1 parent 33112af commit fd09060
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 36 deletions.
Binary file added episodes/fig/dimensions.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added episodes/fig/workspace.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
109 changes: 73 additions & 36 deletions episodes/variables-and-dimensions.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ exercises: 2

::::::::::::::::::::::::::::::::::::: objectives

- Understand what a Variable is and how MATLAB handles them
- Learn about slicing and how we can create variables of various dimensions
- What is a variable and how MATLAB handles them
- How we can create variables of various dimensions
- Briefly review data types and why they aren't important in MATLAB

::::::::::::::::::::::::::::::::::::::::::::::::
Expand All @@ -40,7 +40,6 @@ The value of a variable in MATLAB can be many things! Some examples are
|Matrix| [1 2 3; 4 5 6]|
|Logical| True, False|

## Workspace

As mentioned in the previous episode, MATLAB has a section called the workspace, this is where you can view what Variables are in-memory. The most simple way of creating a variable is with the '=' symbol, for example:
```
Expand All @@ -55,6 +54,7 @@ MATLAB (like most programming languages), holds variables in-memory. This means
:::

::: challenge
### Challenge 1: Creating Variables
1. Create a variable called A with a value of 3: `A = 3`

2. Create a variable called B with a value of hello: `B = 'hello'`
Expand All @@ -73,63 +73,100 @@ MATLAB (like most programming languages), holds variables in-memory. This means
### Data Types
You may notice in the previous solution there is a type called a 'double'. MATLAB is whats known as a dynamically typed language, which is also called the fun duck typing. This is based off the saying 'if it walks like a duck and quacks like a duck it's probably a duck'. In programming terms this means that when you make a variable MATLAB has a look at it and assumes what type it is based on how it looks. Other languages like C++ require you to explicitly tell the program what type every variable is.

For you as a user, this means that you don't have to really know or pay attention to data types! However it is worth knowing they exist as if you get more advanced you may want to manipulate them to optimise your algorithms or some other advanced use cases.
For you as a user, this means that you don't have to really know or pay attention to data types! However it is worth knowing they exist, as if you get more advanced you may want to manipulate them to optimise your algorithms or some other advanced use cases.
:::

::::::::::::::::::::::::::::::::::::: challenge
## Dimensions

## Challenge 1: Can you do it?
A lot of data analysis, processing and workflows wont be with single numbers but with large multidimensional datasets. Working with image/video, timeseries, tables or many other data sources can quickly make your incoming data large in size. Variables in MATLAB can and are very good at storing and working with multidimensional data.

What is the output of this command?

```r
paste("This", "new", "lesson", "looks", "good")
```
::: challenge
### Penny command

:::::::::::::::::::::::: solution
Use the `penny` command in the command window. this command will create an interactive 3d plot of a US penny mold.

## Output

```output
[1] "This new lesson looks good"
```
1. How many new variables are there in your work space?
2. Look at the size column in your workspace, how many dimensions are there in each variable?

:::::::::::::::::::::::::::::::::
::: hint

If you double click a variable name in the workspace you can explore it in a spreadsheet style interface

## Challenge 2: how do you nest solutions within challenge blocks?
:::

:::::::::::::::::::::::: solution
::: solution

You can add a line with at least three colons and a `solution` tag.
There should be 2 new variables in your workspace, `P` and `D`. Both have a size of 128x128, this means they have 128 rows and 128 columns and therefore are 2 dimensional.

:::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::
:::

## Figures
:::

You can use pandoc markdown for static figures with the following syntax:
::: callout
### Dimension Order

`![optional caption that appears below the figure](figure url){alt='alt text for
accessibility purposes'}`
In MATLAB learning the order in which dimensions appear is very helpful.

The first dimension is always rows and the second columns. So if you saw a variable with size 5x10, you could picture it looking like a spreadsheet with 5 rows and 10 columns.
:::

![You belong in The Carpentries!](https://raw.githubusercontent.com/carpentries/logo/master/Badge_Carpentries.svg){alt='Blue Carpentries hex person logo with no text.'}

## Math
In MATLAB there are names given to different variable shapes:

One of our episodes contains $\LaTeX$ equations when describing how to create
dynamic reports with {knitr}, so we now use mathjax to describe this:
| Dimensions | Name | Description |
|---|---|---|
| 0D | Scalar | A single value like those created in Challenge #1|
| 1D | Vector | A single row or column |
| 2D | Matrix | Rows and columns, like an Excel spreedsheet or Google sheet |
| 3D+ | Array | Rows, columns and pages |

`$\alpha = \dfrac{1}{(1 - \beta)^2}$` becomes: $\alpha = \dfrac{1}{(1 - \beta)^2}$
![Animated figure showing a vector, matrix & array](fig/dimensions.gif){alt="An animated GIF showing a vector column, matrix and array being built. It aims to add intuation for the shape of each of these types"}

Cool, right?
### Creating Multidimensional Variables

In general when creating multidimensional variables you
- use square brackets `[]`
- separate columns with a space ` `
- separate rows with a semi-colon `;`

::: challenge

1. Create a 1x3 row vector named E: `E = [1 2 3]`
2. Create a 3x1 column vector called F with values 4 to 6: `F = [4;5;6]`
3. Create a 2x2 matrix called G with values 10, 20, 30 & 40.

::: solution

Solution to 3. is `G = [10 20; 30 40]`

:::
:::

## Cleaning Workspace

Finally we will look at how we can remove variables we aren't using! We do this because as you explore data and test your analysis over time you will get variables you aren't building cluttering your workspace, this can lead to you either accidently overwriting data or using variables you weren't intending to.

**Clearing all variables**

Simply putting `clear` into your command window will clear all variables

**Clear single variable**

`clear A` will clear just the variable called A

**Clear command window**

After working a while you will have a long history of commands in your command window, you can clear this up by typing `clc` (command line clear).

::: callout
You can access commands you have previously typed in the command window by pressing the up arrow on your keyboard
:::
::::::::::::::::::::::::::::::::::::: keypoints

- Use `.md` files for episodes when you want static content
- Use `.Rmd` files for episodes when you need to generate output
- Run `sandpaper::check_lesson()` to identify any issues with your lesson
- Run `sandpaper::build_lesson()` to preview your lesson locally
- Variables are the main way we access and use data in MATLAB
- Variables can store many types of data including multidimensional data
- Creating multidimensional arrays is done with square brackets

::::::::::::::::::::::::::::::::::::::::::::::::

0 comments on commit fd09060

Please sign in to comment.