Working with Variables
Last updated on 2024-11-06 | 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
- Access MATLAB variables and change them
- Use variables to execute mathematical operations and in functions
Introduction
So far we have learnt how to create variables of various sizes with different methods. This episode will now focus on how ways we can use variables.
Now is a good time to clear
and clc
your
workspace and command window!
Extracting Variables
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 multiple values as well:
A single colon with no numbers will select all
Some more examples bringing together the tools we’ve seen so far:
MATLAB
% Extract the first 4 rows and all columns and save it in the variable subset1
subset1 = data(1:4,:)
% Select every other column in the first row, save in subset2
subset2 = data(1,1:2:end)
% Select the first, third and forth rows for all columns, save in subset3
subset3 = data([1 3 4], :)
end
When extracting the subset of a matrix, or slicing it, when
specifying the range you can use the keyword end
to
represent the last value in a vector (or row or column of a matrix,
etc.).
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 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 concatenate them together into a larger column vector there are three ways
MATLAB
new_data = [subset1; subset2]
new_data = cat(1, subset1, subset2)
new_data = vertcat(subset1, subset2)
All [;]
, cat
, and vertcat
are
all different ways to do the same vertical concatenation.
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 concatenate the subsets into 1 variable as separate columns we could do
MATLAB
new_data2 = [subset1 subset2]
new_data2 = cat(2,subset1,subset2)
new_data2 = horzcat(subset1,subset2)
One advantage of using cat
is that it can work for
arrays of larger dimensions, whereas the square bracket shortcut,
vertcat
, and horzcat
only works for the first
two dimensions of the data.
Challenge 1
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 and call it
subset_b
Transpose
subset_b
, call this variablesubset_t
Concatenate
subset_a
andsubset_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 and 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
subset_concatenated = cat(1, subset_a, subset_t)
subset_concatenated
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
clear
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 pay attention to make sure you are getting the result you expect!
Challenge 2
- 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 to have been doing 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
% Find the size of a matrix
matrix_size = size(data)
% 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")
Indian Rainfall Example
In this example we will combine the tools we have learnt so far to compare rainfall data between Sheffield and India
Each dataset comprises of monthly rainfall averages per year from 1901-2014.
Sheffield (https://www.metoffice.gov.uk/pub/data/weather/uk/climate/stationdata/sheffielddata.txt) Peninsular India (https://www.tropmet.res.in/DataArchival-51-Page)
MATLAB
% Import rainfall data.
sheffield_data = load('Sheffield_Rain.csv');
india_data = load('SouthIndia_Rainfall.csv');
Challenge
Investigate the data, open it up in the workspace and think about what is in each column
Each dataset contains 13 columns, the first column is the year and the other 12 are for each month of the year
Challenge
Create a subset of
sheffield_data
andindia_data
that contains only the rain data, call these subsetssheffield_rain
andindia_rain
The Indian rainfall series is in tenths of a millimeter. Convert to millimeters by dividing each point by 10
The columns contain monthly rainfall, find the average monthly rainfall over the 114 years. Call these new variables
india_monthly
andsheffield_monthly
MATLAB
% Select all data except the first column
sheffield_rain = sheffield_data(:,2:end);
india_rain = india_data(:,2:end);
% Convert india_rain to millimeters
india_rain = india_rain/10;
% Take the mean of each column to find the average monthly rainfall
sheffield_monthly = mean(sheffield_rain, 1);
india_mothly = mean(india_rain, 1);
If your sheffield_monthly
and india_monthly
variables are correctly made, you should be able to run the following
code to generate a bar chart comparing the two average rainfalls.
MATLAB
bar([1:12],cat(1, india_mothly, sheffield_monthly),'grouped')
legend('South India', 'Sheffield')
ylabel('Rainfall (mm)')
Key Points
- Use functions to create, edit and operate on variables
- Operations are used to perform simple mathematical functions
- help and docs are valuable tools for understanding functions