Financial Toolbox | ![]() ![]() |
Matrix Input
Because MATLAB can process vectors and matrices easily, most functions in the Financial Toolbox allow vector or matrix input arguments, rather than just single (scalar) values.
For example, the irr
function computes the internal rate of return of a cash flow stream. It accepts a vector of cash flows and returns a scalar-valued internal rate of return. However, it also accepts a matrix of cash flow streams, a column in the matrix representing a different cash flow stream. In this case, irr
returns a vector of internal rates of return, each entry in the vector corresponding to a column of the input matrix. Many other toolbox functions work similarly.
As an example, suppose you make an initial investment of $100, from which you then receive by a series of annual cash receipts of $10, $20, $30, $40, and $50. This cash flow stream may be stored in a vector
The irr
function can compute the internal rate of return of this stream.
The internal rate of return of this investment is
In this case, a single cash flow stream (written as an input vector) produces a scalar output - the internal rate of return of the investment.
Extending this example, if you process a matrix of identical cash flow streams
you should expect to see identical internal rates of return for each of the three investments.
This simple example illustrates the power of vectorized programming. The example shows how to collect data into a matrix and then use a toolbox function to compute answers for the entire collection. This feature can be useful in portfolio management, for example, where you might want to organize multiple assets into a single collection. Place data for each asset in a different column or row of a matrix, then pass the matrix to a Financial Toolbox function. MATLAB performs the same computation on all of the assets at once.
Matrices of String Input
Enter strings in MATLAB surrounded by single quotes ('string'
).
Strings are stored as character arrays, one ASCII character per element. Thus the date string
is actually a 1-by-9 vector. Strings making up the rows of a matrix or vector all must have the same length. To enter several date strings, therefore, use a column vector and be sure all strings are the same length. Fill in with spaces or zeros. For example, to create a vector of dates corresponding to irregular cash flows
DateFields actually becomes a 5-by-10 character array.
Don't mix numbers and strings in a matrix. If you do, MATLAB treats all entries as characters. For example,
becomes a 1-by-14 character array, not a 1-by-4 vector, and it contains
![]() | Function Input/Output Arguments | Function Output Arguments | ![]() |