MATLAB Notes
MATLAB Notes
rand/randn/randi
The uniformly distributed random integers generated by randi lie
between one and the specified maximum value by default.
You can specify the interval over which the random integers are
distributed using
x = randi([minVal maxVal],m,n)
This code will generate random integers between minVal and maxVal.
It also works in this way: x = randi(maxvalue,m,n)
MATRIX MULTIPLICATION
A.*B gives the same result of A*B if the number of columns of A is
the same number of rows in B.
We can use .* (or ./) when we want to operate between an element in
A and the correspondent in B (element-wise multiplication). A and B
must have compatible sizes(Two inputs have compatible sizes if, for
every dimension, the dimension sizes of the inputs are either the
same or one of them is 1). MATLAB implicitly expands arrays with
compatible sizes to be the same size during the execution of the
element-wise operation or function .
isnan/max/min/mean/nnz/sort/diff/cumsum
You can identify and count missing values in a vector. To identify
missing values in a vector, use the isnan function.
isnan(v)
The output is a logical vector where missing values are marked with
a 1 for true.
You can count the number of missing values with the nnz (number non-
zero) function.
nnz(vLogical)
You can ignore missing values in the mean calculation by using the
"omitnan" flag.
mean(v,"omitnan")
You can use the max function to calculate the largest element of a
vector and the index where it occurs by requesting two outputs.
[m,idx] = max(v);
You can sort data from largest to smallest using "descend" option
for the sort function.
[xSorted,idx] = sort(v,"descend")
The sort function orders arrays from smallest to largest by default.
You can achieve the same result by passing "ascend" as the second
input to sort.
If there are missing values:
You can specify whether you want the missing values at the top or
bottom with the MissingPlacement option.
[desc,idx] = sort(usageDiff,"descend",...
"MissingPlacement","last")
The min and max functions also calculate the minimum and maximum of
the columns of a matrix. They omit missing values by default, so
you only need to provide the matrix.
You can also get the mean, minimum, and maximum, of all of the
elements in array by specifying the dimension as "all".
minUse = min(usage,[],'all')
maxUse = max(usage,[],'all')
avgUse = mean(usage,'all','omitnan')
For the max and min functions, you can request a second output that
gives you the index for the maximum of each column
The cumsum function calculates the sum of an element with all the
previous elements of a vector. If the input is a matrix, the sums
are calculated by column, and the output is a matrix of the same
size.
cumsum(M,”omitnan”)
If you want to operate by row:
d=diff(M,[],2)
bar(sum(d,’omitnan’))
PLOT
stairs/stem/area/scatter/bar/plot/surf/mesh/contour/histogram
Stairs plot:
stairs(xdata,ydata)
Stem plot:
stem(xdata,ydata)
Area plot:
area(xdata,ydata)
You can change the angle of the text using the xtickangle function.
xtickangle(15)
ylabel(txt)
If we want to create a multiline annotation the string vector has be
a column vector
e.g. ylabel(["Area";"\pi r^2"])
histogram(data)
The histogram function can bin on integer values. Use the BinMethod
property name and specify the value as "integers".
histogram(data,"BinMethod","integers")
cassic
plot:
To specify marker size, face color:
plot(x,y,’o’, MarkerSize=10, MarkerFaceColor=’g’)
Other properties: Linewidth, MarkerEdgeColor
To plot a line having a specific color set the property “Color” to a
RGB vector: e.g. plot(x,y,Color=[0.5 0.6 0])
If we use strings properties:
plot(x,y,'o-', "Color",[.6 .2 .8], "MarkerEdgeColor", 'c')
Surf(x,y,Z)
To specify x and y coordinates, you can pass them in as vectors.
The number of elements of x must match the number of columns of z
The number of elements of y must match the number of rows of z
LOGICAL COUNTING
any/nnz/all
any(logical vector)
You can use any to find if any of the elements in a logical array
are true
You can use the function all to find if all the elements in a
logical vector are true. It operates by column if it is a matrix
all(logical vector)
To operate row-wise: all(v, 2)
You can use the function nnz to count the Number of Non-Zero values
in an array, or the number of true values in an array.
nnz(v)
It always gives a scalar number, regardless it is a vector or matrix
LOGICAL INDEXING
find
You can use the function find to find the indices of the true values
in a logical vector.
v = [false true true false];
idx = find(v)
idx =
[2 3]
You can obtain the locations of just the first or last n true values
in a logical vector by providing optional extra arguments to find.
idx = find(v,n,dirn)
where dirn is either "first" or "last".
TABLES OF DATA
readtable/summary/table/array2table
Use the readtable function to import the data from a file into a
table in MATLAB.
data = readtable("dataFile.txt")
To ensure text is imported as a string, set the TextType property to
string.
data = readtable("dataFile.txt","TextType","string")
You can use the summary function to display a summary of each
variable.
summary(data)
You can organize your workspace variables into a table with the
table function. The following code creates a table, data with
variables a, b, and c.
data = table(a,b,c)
data = table(a,b,c, ‘VariableNames’, {‘Var1’,’Var2’, ‘Var3’})
It can also works in this way:
data = table(a,b,VariableNames=[“Var1”, “Var2”]) variable names must
be string if included in [], otherwise:
data 0 table(a,b,VariableNames={‘Var1’, ‘Var2’})
IMPORTANT: the table function works with VariableNames parameter
specified as character vector (i.e ‘VariableNames’), and the
variable names coming after must be included in a cell array of
character vectors((i.e.{‘var1’, ‘var2’})
You can use dot notation to extract data for use in calculations or
plotting.
You can also use dot notation to create new variables in a table.
tw = EPL.HW + EPL.AW
SORTING TABLE
sortrows
You can sort a table on a specific variable using the sortrows
function.
tSort = sortrows(tableName,"SortingVariable")
You can use the movevars function to move a variable (or variables)
to the position "Before" or "After" another variable.
T = movevars(T,vars2move,"After",posVar)
You can reference vars2move and posVar using numeric or named
indexing.
Try using movevars to move the team names to the end of the table.
EPL = movevars(EPL,"Team","After","AwayLosses")
You can specify a delimiter, such as tab, when creating the file.
writetable(data,"fileName.txt","Delimiter","\t")
You can use the join function to merge two tables using any
variables with the same names as key variables.
newTable = join(table1,table2)
The join function matches up rows using variables with the same name
as key variables. All the variables from the first entry are
retained, and then it adds corresponding information from the non-
key variables of the second entry.
The order matters!
You can access metadata from a table using the Properties property.
tableName.Properties
You can also access specific table properties using dot notation.
tableName.Properties.propName
CELL ARRAYS
Index into a cell array using parentheses, (), to return a cell
array subset.
The following code will return the first row of regions.
countries = regions(1,:)
countries =
1×2 cell array
{'South America'} {'Europe'}
To represent an interval
duration
If you subtract a datetime value from another one you’ll get a
duration time in hours.
To transform it in days:
days(durationvalue)
If the input of days is a duration it outputs a scalar. Otherwise,
if the input is a scalar it converts in a duaration variable
You can create a duration time by using the functions days, hours,
minutes, etc.
There are functions years and days, but what happens if you try
months?
months(seasonEnd - seasonStart)
Error using duration/months
Durations cannot represent calendar months.
CATEGORICALS ARRAYS
categorical/categories/mergecats/summary
You can convert a string array to a categorical using the
categorical function.
catArray = categorical(stringArray);
You can use == for comparison the same way you would with string
arrays (or other logical function)
You can view the category names with the number of elements in each
category using the summary function.
summary(players.Team)
By default, the possible values and the names of the categories will
be determined automatically from the data. You can specify these as
additional inputs to categorical, where the second input indicates
the unique category values in the original array, and the third
input indicates the names that correspond to these categories.
v = [ 10 5 0 0 ];
levels = ["beg" "mid" "last"];
categorical(v,[0 5 10],levels)
ans =
last mid beg beg
v = [ 10 5 0 0 ];
levels = ["beg" "mid" "last"];
c = categorical(v, [0 5 10],levels,"Ordinal",true)
c > "mid"
ans =
1 0 0 0
PREPROCESSING DATA
Data normalization: normalize
One of the most common ways to normalize data is to shift it so that
it's mean is centered on zero (i.e. the data has zero mean) and
scale the data so that it's standard deviation is one. This is
called the z-score of the data.
xNorm = normalize(x)
xScale = normalize(X,"scale","first")
or
xScale = normalize(X, "scale",X(1,:))
You can also stretch or compress data so that it's maximum and
minimum values are within a specified range [a,b].
xRange = normalize(X,"range",[a b])
INTERPOLATION
SMOOTHING DATA
You can smooth the data using the function as follows
smoothedData = smoothdata(y,"movmean",3,"SamplePoints",x);
When data is evenly sampled there’s no need to add the sample points
(e.g. 3).
The function works column-wise
You can specify the number of points backwards and forward of the
window
y = smoothdata(x,method,[kb kf])
e.g. Create a vector named lead4med that contains the leading 4-
point median of y (i.e., the median of the current point and the
three next points).
lead4med = smoothdata(y,"movmedian", [0 3])
Smoothdata is a function to filter data (lowpass, gaussian, etc.)
The command yyaxis left creates new axes with independent vertical
axis scales. The axis on the left is currently active.
yyaxis left
The command yyaxis right changes the currently active axis to the
axis on the right. Plots are now created in this axis which uses a
different scale to the axis on the left.
yyaxis right
plot(t,y2)
Issuing the command yyaxis left a second time does not modify the
axis on the left but makes it active again, allowing you to make
modifications to the axes without replotting.
yyaxis left
ylabel("y_1")
ylim([0 20])
Similarly, yyaxis right makes the axis on the right active again.
yyaxis right
ylabel("y_2")
ylim([0 600])
xlabel("x")
LINEAR CORRELATION
To see if there is a linear correlation betwenn two or more
variables use corrcoef
corrcoef(v1,v2)
The function returns in this case a 2x2 matrix with values in the
range [-1 1] where 1 means perfect correlation(diagonal), -1
anticorrelation, 0 no correlation
Scatterplot can help visualize the correlation between the
variables. If we want to see more than 2 variables we can create a
scattter for each pair with the plotmatrix function
plotmatrix(matrix)
The plots in the diagonal are the histogram of the individual
columns.
You can use corrcoef on a matrix by passing it as input. The result
will be a matrix with the same size.
Corrcoef([a b c])
The polyfit function finds the coefficients of the best fit n-th
degree polynomial of yData in terms of xData.
cf = polyfit(xData,yData,n)
yFit = polyval(c,xFit,[],sc)
where sc is the vector of the scaled coefficients
PROGRAMMING CONSTRUCTS
warning(text) to display a warning
disp(text) to display info
When it is important to make sure the user saw your message, you can
create dialog boxes that display messages, errors, or warnings. The
corresponding functions are msgbox, errordlg, and warndlg. Each of
them accepts two inputs: the message and a title for the dialog box.
msgbox("Analysis complete","Complete!")
errordlg("Data not found","Error!")
warndlg("Missing values.","Warning!")
IF
if condition
code
else
code
end
or
if condition
code
elseif condition1
code
elseif condition2
code
else
code
end
Switch-case
switch x
case 1
case 2
end
FOR
idx=1
for idx=1:100
code
end
WHILE
while condition is true
code
end
FUNCTIONS
You can define local functions at the end of a script. Your script
can call any local function defined within it. However, as the name
implies, these functions are local to that script – they are not
accessible from outside the script where they are defined. You
cannot call a local function from the Command Window or another
script or function.
A function can also contain its own local functions, if desired. As
always, these local functions are accessible only within the file.
That is, they can be called only by the primary function. The
primary function is always the first function in the file and its
name matches the file name.
The syntax is always:
function [outputs] = functname(input)
code
end
Variable priority
First Matlab checks the base workspace, then a local function, a
file in the current folder, then a file in other local drive in your
pc.Matlab has a search path so to know where to find the function.
To add folders to the search path:
On the Home tab, in the Environment section, click Set Path.
Add a single folder or a set of folders using the buttons
highlighted below.You can also add the path in the workspace.
which variable -all
to know what the variable is in Matlab