Skip to content

Commit

Permalink
Merge variable episodes
Browse files Browse the repository at this point in the history
  • Loading branch information
christopher-wild committed Oct 29, 2024
1 parent 926754c commit 54bf92a
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 139 deletions.
9 changes: 4 additions & 5 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -58,22 +58,21 @@ contact: '[email protected]'
# - another-learner.md

# Order of episodes in your lesson
episodes:
episodes:
- introduction.md
- what-is-matlab.md
- matlab-layout.md
- variables-and-dimensions.md
- creating-variables.md
- manipulating-variables.md

# Information for Learners
learners:
learners:

# Information for Instructors
instructors:
instructors:

# Learner Profiles
profiles:
profiles:

# Customisation ---------------------------------------------
#
Expand Down
132 changes: 0 additions & 132 deletions episodes/creating-variables.md

This file was deleted.

114 changes: 112 additions & 2 deletions episodes/variables-and-dimensions.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ The value of a variable in MATLAB can be many things! Some examples are
|Matrix| [1 2 3; 4 5 6]|
|Logical| True, False|

## TODO: variable naming convention

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 Down Expand Up @@ -91,7 +92,7 @@ Use the `penny` command in the command window. this command will create an inter

::: hint

If you double click a variable name in the workspace you can explore it in a spreadsheet style interface
If you double click a variable name in the workspace you can explore it in a spreadsheet style interface, this is called the variable editor.

:::

Expand Down Expand Up @@ -130,6 +131,7 @@ In general when creating multidimensional variables you
- separate columns with a space ` `
- separate rows with a semi-colon `;`

## TODO: make this read nicer, all in challenge?
::: challenge

1. Create a 1x3 row vector named E: `E = [1 2 3]`
Expand All @@ -156,12 +158,120 @@ Simply putting `clear` into your command window will clear all variables
`clear A` will clear just the variable called A

**Clear command window**

## TODO clearup history
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
:::


## Indexing

### Colon Notation

MATLAB provides what is called the colon notation which allows us to specify a range of values.

``` MATLAB
a = [1:10]
```
``` OUTPUT
a =
1 2 3 4 5 6 7 8 9 10
```

`a` should be 1x10 size, meaning 1 row 10 columns

### Steps
You can also specify a step, so the colon notation only makes every nth number

``` MATLAB
b = [1:2:20]
```

``` OUTPUT
b =
1 3 5 7 9 11 13 15 17 19
```

## Functions

We can also use functions to create arrays. Functions are premade code blocks that serve a commonly wanted functionality.

First we will use one called linspace, which stands for linearly spaced. It creates a vector of linearly spaced numbers, you specify the start, end and how many numbers.

::: callout

If you are ever unsure about a function MATLAB has inbuilt help and documentation!

The help command will give a brief text description about the function and how to use it
``` MATLAB
help linspace
```

The doc command will give a more detailed description including examples
``` MATLAB
doc linspace
```

:::

``` MATLAB
c = linspace(1,10,5)
```

``` OUTPUT
c =
1.0000 3.2500 5.5000 7.7500 10.0000
```

Some other useful functions are:
``` MATLAB
% Rand creates an n-dimensional array of random numberes between 0 and 1.
d = rand(5,1)
% Create matrix of 0's
e = zeros(2,2)
% NaN means 'Not a Number', this is a special and useful term for when a value can't be represented by a number.
f = nan(5,5)
```
::: callout

Here are some common scenarios where NaN may be used:

- Mathematical operations that cant be computed (division by 0, root of negative numbers)
- No data was recorded, for example a in-field sensor may have lost power

:::

::: challenge

1. Create a vector with numbers 12 to 100 containing every 4th number (steps of 4), call it g
2. Create a matrix of random numbers with 4 rows and 5 columns, call it h

::: solution
``` MATLAB
g = 12:4:100
or
g = linspace(12,100,23)
```
:::

:::



::::::::::::::::::::::::::::::::::::: keypoints

- Variables are the main way we access and use data in MATLAB
Expand Down

0 comments on commit 54bf92a

Please sign in to comment.