Following my earlier posts What’s New in MATLAB R2016b? and What’s New in MATLAB R2017a? I take a look here at the R2017b release of MATLAB. As before, this is not a comprehensive treatment (for which see the Release Notes), but rather a brief description of the changes in MATLAB (not the toolboxes) that are the most notable from my point of view.
Decomposition Object
MATLAB now has a way of avoiding unnecessarily repeating the factorization of a matrix. In the past, if we wanted to solve two linear systems Ax = b
and Ay = c
involving the same square, general nonsingular matrix A
, writing
x = A\b; y = A\c;
was wasteful because A
would be LU factorized twice. The way to avoid this unnecessary work was to factorize A
explicitly then re-use the factors:
[L,U] = lu(A); x = U\(L\b); y = U\(L\c);
This solution lacks elegance. It is also less than ideal from the point of view of code maintenance and re-use. If we want to adapt the same code to handle symmetric positive definite A
we have to change all three lines, even though the mathematical concept is the same:
R = chol(A); x = R\(R'\b); y = R\(R'\c);
The new decomposition
function creates an object that contains the factorization of interest and allows it to be re-used. We can now write
dA = decomposition(A); x = dA\b; y = dA\c;
MATLAB automatically chooses the factorization based on the properties of A
(as the backslash operator has always done). So for a general square matrix it LU factorizes A
, while for a symmetric positive definite matrix it takes the Cholesky factorization. The decomposition
object knows to use the factors within it when it encounters a backslash. So this example is functionally equivalent to the first two.
The type of decomposition can be specified as a second input argument, for example:
dA = decomposition(A,'lu'); dA = decomposition(A,'chol'); dA = decomposition(A,'ldl'); dA = decomposition(A,'qr');
These usages make the intention explicit and save a little computation, as MATLAB does not have to determine the matrix type. Currently, 'svd'
is not supported as a second input augment.
This is a very welcome addition to the matrix factorization capabilities in MATLAB. Tim Davis proposed this idea in his 2013 article Algorithm 930: FACTORIZE: An Object-Oriented Linear System Solver for MATLAB. In Tim’s approach, all relevant functions such as orth
, rank
, condest
are overloaded to use a decomposition object of the appropriate type. MATLAB currently uses the decomposition object only in backslash, forward slash, negation, and conjugate transpose.
The notation dA
used in the MathWorks documentation for the factorization object associated with A
grates a little with me, since dA
(or ) is standard notation for a perturbation of the matrix
A
and of course the derivative of a function. In my usage I will probably write something more descriptive, such as decompA
or factorsA
.
String Conversions
Functions written for versions of MATLAB prior to 2016b might assume that certain inputs are character vectors (the old way of storing strings) and might not work with the new string arrays introduced in MATLAB R2016b. The function convertStringsToChars
can be used at the start of a function to convert strings to character vectors or cell arrays of character vectors:
function y = myfun(x,y,z) [x,y,z] = convertStringToChars(x,y,z);
The pre-2016b function should then work as expected.
More functions now accept string arguments. For example, we can now write (with double quotes as opposed to the older single quote syntax for character vectors)
>> A = gallery("moler",3) A = 1 -1 -1 -1 2 0 -1 0 3
Tall Arrays
There is more support in R2017b for tall arrays (arrays that are too large to fit in memory). They can be indexed, with sorted indices in the first dimension; functions including median
, plot
, and polyfit
now accept tall array arguments; and the random number generator used in tall array calculations can be independently controlled.
Word Clouds
The new wordcloud
function produces word clouds. The following code runs the function on a file of words from The Princeton Companion to Applied Mathematics.
words = fileread('pcam_words.txt'); words = string(words); words = splitlines(words); words(strlength(words)<5) = []; words = categorical(words); figure wordcloud(words)
I had prepared this file in order to generate a word cloud using the Wordle website. Here is the MATLAB version followed by the Wordle version:
Wordle removes certain stop words (common but unimportant words) that
wordcloud
leaves in. Also, whereas Wordle produces a different layout each time it is called, different layouts can be obtained from wordcloud
with the syntax wordcloud(words,'LayoutNum',n)
with n
a nonnegative integer.
Minima and Maxima of Discrete Data
New functions islocalmin
and islocalmax
find local minima and maxima within discrete data. Obviously targeted at data manipulation applications, such as analysis of time series, these functions offer a number of options to define what is meant by a minimum or maximum.
The code
A = randn(10); plot(A,'LineWidth',1.5); hold on plot(islocalmax(A).*A,'r.','MarkerSize',25); hold off, axis tight
finds maxima down the columns of a random matrix and produces this plot:
Whereas min
and max
find the smallest and largest elements of an array, the new functions mink(A,k)
and maxk(A,k)
find the k
smallest and largest elements of A
, columnwise if A
is a matrix.