### This is a summary of a commonly used MATLAB functions, I suggest you to use MATLAB help in order to have the syntax complete view with all the possible option and features.

## size

To find the size of a dimension of a matrix, use z=size(x,DIM), where DIM is the dimension.

**Note that dimension 1 is the number of rows while dimension 2 is the number of columns, **

so if x is T by K, *z=size(x,1)* returns T while *z=size(x,2)* returns K .

alternatively in a better way

>> [m,n] = size (x)

the command will return m = T and n = K.

## length

This command find the size of the maximum dimension of x. YOU WILL NOT get the dimension (BE CAREFUL!).

>> z=length(x).

**Note:** If x is T by K , T > K , z = T . If K > T , z = K.

## sum

To compute the sum matrix,

use the command sum(x).

>> z = sum(x,dim)

dim as always the same meaning as above and in the all MATLAB enviroment.

If dim is equal 1, z will sum up by rows while if it is equal 2 by columns.

## min

To find the minimum element of a vector or the rows of a matrix,

use the command min(x). If x is a vector, min(x) is scalar otherwise if it is a matrix use dim as usual.

>> z = min(x,dim)

## max

It works as min function.

>> z = max(x,dim)

## sort

To sort the values of a vector or the rows of a matrix from smallest to largest, use the command sort(x). If x is a vector, sort(x) is vector where x(1)=min(x) and x(i) < x(i+1).

The complete command,

z = sort(x,dim,mode)

where mode is ‘ascend’ for the ascending order (default) or ‘descend’ for descending order.

## exp

To take the exponential of a vector or matrix (element by element),

use exp.

z=exp(x)

returns a vector or matrix the same size as x where z(i,j)=exp(x(i,j)).

## log

z=log(x)

returns a vector or matrix the same size as x where z(i,j)=log(x(i,j)).

## sqrt

z=sqrt(x)

returns a vector or matrix the same size as x where z(i,j)=sqrt(x(i,j)), you can obtain the same by z=x.^(1/2).

## mean

M=mean(A,dim)

M = mean(A) returns the mean values of the elements along different dimensions of an array by default dim is 1.

## var

To compute the sample variance of a vector or a matrix,

If x is a vector, var(x) is scalar. If x is a matrix, var(x) is a K by 1 vector containing the sample variances of each column.

**Note:** This command uses T – 1 in the denominator unless an optional second argument is used.

>> var(x)

## cov

To compute the sample covariance of a vector or matrix,

use the command

>> cov(x)

It works the same as covariance for the normalization T-1.

## std

It is the equivalent of sqrt(var(x)),

>> std(x)

## skewness

>> skewness(x)

## kurtosis

>> kurtosis(x)

## : operator

The : operator has two uses. One, to access elements in a matrix of vector (e.g. x(1,:)), has described before. *The other is to create a row vector with evenly spaced points. *We have the basic form, *first:last* and the other form *first:increment:last*. The first, first:last, produces a row vector of the form

[first, first + 1, . . . first + N]

where N is the largest integer such that *first+N<= last*

>> x = 1:10 x = 1 2 3 4 5 6 7 8 9 10 >> x = 1:3.5 x = 1 2 3 >> x = -5:4 x = -5 -4 -3 -2 -1 0 1 2 3 4

The second one, first:increment:last, include an increment (positive or negative),

[first, first + increment, first + 2(increment), .. first + N(increment)]

where N is the largest integer such that *first+N(increment)<= last*

>> x = -1:-1:-10 x = -1 -2 -3 -4 -5 -6 -7 -8 -9 -10 >> x = 0:.1:1 x = 0.1000 0.2000 0.3000 0.4000 0.5000 0.6000 0.7000 0.8000 0.9000 1.0000 >> x = -1:.25:1 x = -1.0000 -0.7500 -0.5000 -0.2500 0 0.2500 0.5000 0.7500 1.0000

## linspace

similar to : *operator*, rather than producing a row vector with a predetermined increment, it produce a row vector with a predetermined number of *n* nodes (including lower and upper bound).

>> linspace(0,1,5) ans = 0 0.2500 0.5000 0.7500 1.0000 >> linspace(0,1,6) ans = 0 0.2000 0.4000 0.6000 0.8000 1.0000

## diff

Y = diff(X) calculates differences between adjacent elements of X.

>> Y = diff(x,n,dim)

where n is the number of differentiations (1 by default) and dim as usual defines rows by 1 and columns by 2.

If it is a matrix is the same to compute[x(2:end,:)-x(1:end-1,:)] while if it is a vector [X(2)-X(1) X(3)-X(2) … X(n)-X(n-1)].

x = [1 2 3 4 5]; y = diff(x) y = 1 1 1 1 z = diff(x,2) z = 0 0 0

## Exercises

- Load the “Price” matrix previously saved.
- Create the “Return” matrix (approximate it by logarithmic difference)
- Compute mean, covariance, skewness and kurtosis of “Return” matrix.
- Compute the min, max of data returns.
- Compute manually without using the specific functions, mean, covariance skewness and kurtosis.