Matlab Fundamental 12
Matlab Fundamental 12
Matlab Fundamental 12
TASK plot(dates,usage,'-.')
Plot the columns of usage (residential,
legend(sectors)
commercial, industrial, and total) as
separate lines versus dates. Add a
legend to your plot with the names of
the sectors.
Statistical functions Shifting the columns of a matrix means from the matrix.
like mean act on the so that each column has zero If possible, MATLAB "expands" the row
columns (or, optionally, mean requires subtracting the vector to have the same dimensions as
rows) of a matrix row vector of means from the the matrix.
independently. matrix.
The result is a vector.
If possible, MATLAB "expands" the row vector to have the same dimensions
as the matrix.
The result is an element-wise matrix operation.
Recall that statistical functions, such as sum, work on each column colavg = mean(x)
of a matrix independently, by default.
TASK
Create a row vector colavg that holds the mean of each column of
x.
Recall that you can use an optional dimension argument to specify that a rowsum = sum(x,2)
statistical function acts on the rows instead of the columns.
y = sum(x,2)
TASK
Create a column vector rowsum that holds the sum of each row of x.
You can shift the columns of a matrix independently by subtracting a row y = x – colavg
vector from a matrix. MATLAB automatically expands the vector to match
the dimensions of the matrix.
TASK
Create a matrix y by subtracting the mean of each column of x from that
column.
You can scale the rows of a matrix independently by dividing a matrix by a z = x./rowsum
column vector element-wise. MATLAB automatically expands the vector to
match the dimensions of the matrix.
TASK
Normalize x by dividing each row by its sum. Store the result in a matrix z.
The matrix usage contains the electricity usage data for four sectors for This code imports the data.
each month. The vector dayspermonthcontains the number of days in load electricityData
each month. You can now use these to normalize the electricity usage whos
data by number of days.
TASK This code finds the number of days in each
Use the vector dayspermonth that contains the number of days in each month.
month to normalize the data from monthly totals to daily averages dayspermonth =
(monthly total divided by number of days). Reassign the result to the eomday(year(dates),month(dates))
matrix usage. usage = usage ./ dayspermonth
Summary: Normalizing Matrix Data
1. Apply statistical function to rows or columns of a matrix. The result is a vector.
2. Apply array operation to the matrix and the vector. The vector is "expanded" as
necessary to make the operation feasible.
normalize
Normalize data using a specified normalization method.
The electricity data shown below is incomplete: there are months where some of the usage values are left blank. When
imported, these locations are automatically filled with the value NaN.
Any calculation involving NaN results in NaN. There are three ways to work around this, each with advantages and
disadvantages:
Leave the data as is and ignore Maintains the integrity of the data but can be difficult to implement for
any NaN elements when performing involved calculations.
calculations.
Remove all NaN elements from the data. Simple but, to keep observations aligned, must remove entire rows of
the matrix where any data is missing, resulting in a loss of valid data.
Replace all NaN elements in the data. Keeps the data aligned and makes further computation
straightforward, but modifies the data to include values that were not
actually measured or observed.
TASK
The matrix usage has been loaded into the workspace.
Create a row vector emean that contains the means of
the columns of usage.
y = mean(x,'omitnan')
TASK
Use the 'omitnan' flag to create a row
vector emean that contains the means of the columns
of usage, but ignoring all elements containing NaN.
TASK Z(idx,:) = []
Remove all rows of the matrix Z that contain
any NaNvalues.
Quiz X(ismissing(x)) = []
Which command removes all NaN elements from the
array x?
TASK
Each column of the matrix usage contains electricity
emean = mean(usage, 'omitnan')
usage for a given sector. Create a row
vector emean containing the mean usage for each
sector, ignoring NaN elements.
Remove all rows from usage that contain Remove NaN rows and recalculate
any NaN elements. Calculate the mean usage by sector idx = any(ismissing(usage),2);
of the resulting matrix, and store the result in emean.
usage(idx,:) = [];
emean = mean(usage)
ismissing(x,[-999,NaN])
ans =
Specifying the set of missing values ensures 1×7 logical array
that ismissing identifies all the missing elements. 0 1 0 0 1 0 1
xNaN = standardizeMissing(x,-999)
Use the standardizeMissing function to convert all missing xNaN =
values to NaN. 2 NaN 5 3 NaN 4 NaN
Outputs
y Array of data.
Inputs
TASK plot(y,'-o')
Plot y with circular markers and a solid
line. Use hold on and hold off to add
this plot to the existing figure.
TASK z = fillmissing(x,'pchip')
Create a vector z that
uses 'pchip' (piecewise cubic Hermite)
interpolation to replace the NaN values
of x.
hold off
12.4 Interpolating Missing Data:
(3/6) Filling Missing Electricity Usage
Values
TASK hold on
Plot the residential usage (first column
plot(dates,linfill(:,1),'o')
of linfill) as a function of dates with
circular markers and no line. Add this hold off
plot to the existing figure.
TASK hold on
Plot the residential usage (first column
plot(dates,cubefill(:,1),'x')
of cubefill) as a function
of dates with 'x' markers and no line. hold off
Add this plot to the existing figure.
yinterp =
fillmissing(y,'method',...
'SamplePoints',x)
TASK
Use the fillmissing function to replace
the NaNvalues of y, using linear
interpolation with x locations given by x.
Save the result in a vector called z.
TASK hold on
Plot z as a function
plot(x,z,'x-')
of x with 'x' markers and a solid line.
Add this plot to the existing figure. hold off
Method Meaning
'next' The missing value is the same as the next nonmissing value in the data.
Method Meaning
'previous' The missing value is the same as the previous nonmissing value in the data.
'nearest' The missing value is the same as the nearest (next or previous) nonmissing value in the data.
'linear' The missing value is the linear interpolation (average) of the previous and next nonmissing values.
'spline' Cubic spline interpolation matches the derivatives of the individual interpolants at the data points. This
results in an interpolant that is smooth across the whole data set. However, this can also introduce
spurious oscillations in the interpolant between data points.
'pchip' The cubic Hermite interpolating polynomial method forces the interpolant to maintain the same
monotonicity as the data. This prevents oscillation between data points.
Because normalize is a statistical
function, it acts on the columns of a
matrix independently.
TASK
Normalize the clean data for each
country such that it has zero mean and
unit standard deviation. Assign the result
to the variable pricesNorm.