Quantcast

Documentation Center

  • Trial Software
  • Product Updates

Using Input and Output Arguments with Functions

Input Arguments

Matrix Input

MATLAB® software was designed to be a large-scale array (vector or matrix) processor. In addition to its linear algebra applications, the general array-based processing facility can perform repeated operations on collections of data. When MATLAB code is written to operate simultaneously on collections of data stored in arrays, the code is said to be vectorized. Vectorized code is not only clean and concise, but is also efficiently processed by the underlying MATLAB engine.

Because MATLAB can process vectors and matrices easily, most Financial Toolbox™ functions 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

CashFlows = [-100 10 20 30 40 50]'

which MATLAB displays as

CashFlows =
  -100
    10
    20
    30
    40
    50

The irr function can compute the internal rate of return of this stream.

Rate = irr(CashFlows)

The internal rate of return of this investment is

Rate =

    0.1201

or 12.01%.

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

Rate = irr([CashFlows CashFlows CashFlows])

you should expect to see identical internal rates of return for each of the three investments.

Rate =

    0.1201    0.1201    0.1201

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 MATLAB strings surrounded by single quotes ('string').

Strings are stored as character arrays, one ASCII character per element. Thus, the date string

DateString = '9/16/2001'

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 = ['01/12/2001'
              '02/14/2001'
              '03/03/2001'
              '06/14/2001'
              '12/01/2001'];

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,

Item = [83  90  99 '14-Sep-1999']

becomes a 1-by-14 character array, not a 1-by-4 vector, and it contains

Item =

SZc14-Sep-1999

Output Arguments

Some functions return no arguments, some return just one, and some return multiple arguments. Functions that return multiple arguments use the syntax

[A, B, C] = function(variables...)

to return arguments A, B, and C. If you omit all but one, the function returns the first argument. Thus, for this example if you use the syntax

X = function(variables...)

function returns a value for A, but not for B or C.

Some functions that return vectors accept only scalars as arguments. Why could such functions not accept vectors as arguments and return matrices, where each column in the output matrix corresponds to an entry in the input vector? The answer is that the output vectors can be variable length and thus will not fit in a matrix without some convention to indicate that the shorter columns are missing data.

Functions that require asset life as an input, and return values corresponding to different periods over that life, cannot generally handle vectors or matrices as input arguments. Those functions are:

amortize

Amortization

depfixdb

Fixed declining-balance depreciation

depgendb

General declining-balance depreciation

depsoyd

Sum of years' digits depreciation

For example, suppose you have a collection of assets such as automobiles and you want to compute the depreciation schedules for them. The function depfixdb computes a stream of declining-balance depreciation values for an asset. You might want to set up a vector where each entry is the initial value of each asset. depfixdb also needs the lifetime of an asset. If you were to set up such a collection of automobiles as an input vector, and the lifetimes of those automobiles varied, the resulting depreciation streams would differ in length according to the life of each automobile, and the output column lengths would vary. A matrix must have the same number of rows in each column.

Interest Rate Arguments

One common argument, both as input and output, is interest rate. All Financial Toolbox functions expect and return interest rates as decimal fractions. Thus an interest rate of 9.5% is indicated as 0.095.

Was this topic helpful?