Manipulating Variables
Last updated on 2024-10-17 | Edit this page
Overview
Questions
- How can I fetch data out of a variable?
- How can I change the values of parts of variables?
- What are operations?
Objectives
- Explain how to use markdown with the new lesson template
- Demonstrate how to include pieces of code, figures, and nested challenge blocks
Introduction
So far we have learnt how to create variables of various sizes with different methods. This episode will now focus on how can we use variables? ## Extracting Variables
Now is a good time to clear and clc your workspace and command window!
First lets start by making a dummy variable that we can use as our stand-in dataset.
Subsets
Subsets of values or single values can be extracted from a variable
with round brackets ()
For example to take the value on the third row and second column:
We can use colon notation to extract values as well:
A single colon with no numbers will select all
Some more examples bringing together the tools we’ve seen so far:
Altering Variables
As we have seen so far, the object on the left hand side of the equal sign ‘=’ is set to what is on the right hand side. So to change the value of a variable we simply put it on the left:
If you look at data in your workspace now you will see that value has been changed!
One use case of altering data may be when you find an erroneous value. For example if you were looking at a table of reviews out of 5, you may wish to change a rating that was somehow set to above 5 to NAN, to show it was invalid.
Transpose
One useful tool in manipulating matrices (plural of matrix) in MATLAB is the transpose. This will effectively pivot the matrix so each row becomes a column and each column becomes a row. This is done in MATLAB by adding an apostrophe after a variable:
You should see that data_t has a flipped size compared to data
Concatenation
Concatenation (also called concat or cat) is a common operation in data handling. Concatenating means to link or put together, it allows to to take two matricies or variables and add them into a single variable. This is useful for if example your dataset is saved across multiple files.
First let’s clear our workspace again, create a new data variable and some subsets of the data to work with.
Both our subsets are column vectors, if we wanted to concaternate them together into a larger column vector there are 2 ways
Callout
Don’t forget if you find a function you aren’t familiar with you can
use help
or doc
to learn more!
If we wanted to concat the subsets into 1 variable as separate columns we could do
One advantage of using cat is that it can work for arrays of larger dimensions, where the square bracket shortcut only works for up to 2D data.
Challenge
Extract every other row from Data assign it to the varibale name subset_a
Extract the first four rows from the 2nd column of Data. Call it subset_b
Transpose subset_b, call this variable subset_t
Concatenate subset_a and subset_t along the first dimension
MATLAB
% Extract every other row from Data assign it to the varibale name subset_a
subset_a = Data(1:2:6,:)
% Extract the first four rows from the 2nd column of Data. Call it subset_b
subset_b = Data(1:4,2)
% Transpose subset_b, call this varibale subset_t
subset_t = subset_b'
% Concatenate subset_a and subset_t along the first dimension
newdata = cat(1, subset_a, subset_t)
newdata should be of size 4x4
Operations
We’re now going to look at some mathematical operations we can perform on our variables.
Before continuing this is a good point to clear your workspace again and make a new dummy data variable. This time we will round each data point to the nearest whole number with the function round
MATLAB
data = 100 * rand(10,10)
% Round to nearest whole number and overwrite data variable
data = round(data)
You may be familiar with the syntax (syntax is a word typically used in programming to mean format) of the most basic operators from other computer programs.
MATLAB
data_add = data + 10;
data_subtract = data - 10;
data_divide = data / 10;
data_multiply = data * 10;
One common mistake made by users of MATLAB is with the multiply operator. When multiplying by a single number like above the behaviour may be as you except.
Challenge
- make a row vector called row with values 1, 2 & 3
- make a column vector called column with values 4, 5 & 6
- before trying to multiply them, guess the size of the result of row*column
- multiply row and column and see if the result is as you expect
As seen in the challenge above, by default MATLAB will attempt to perform something called matrix multiplcation, you don’t need to know about matrix multiplication but it is worth knowing it is the default behaviour.
What you may expect is something called dot multiplication
OUTPUT
ans =
4 10 18
As the example above shows, dot multiplication multiplies each element of both variables with each other 1 to 1. This is why it is also sometimes called element-wise multiplication.
functions
Next we will look at some key functions that you may want to use in data analysis and processing
MATLAB
% Add together the rows in each column
column_totals = sum(data, 1)
% Take the mean of the rows in each row
row_means = mean(data, 2)
% Find the maximum value in each column
data_max = max(data)
% Find the minimum value in each row
data_min = min(data, [], 2)
% Find the maximum value of the entire matrix
data_max_all = max(data, [], "all")
Key Points
- 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