# Tutorial Video

# Inbuilt Functions with Multiple Input Forms

Previously we looked at some functions which could be used on Scalar, Vector or Matrix Inputs. If a Scalar Input was selected then a Scalar Output was selected. If a Vector or Matrix input was selected then a corresponding Vector or Matrix output was created with the function acting on the input Matrix, element by element.

Some other functions however act differently depending on the presence of absence of additional inputs.

# max function

`[Output]=max(M,N,direction)`

Let's create two different Matrices of equal dimensions:

`M=[1,4,5;7,10,11;13,15,18]`

`N=[2,3,6;8,9,12;14,16,17]`

We can use the function:

`[Output]=max(M,N)`

To get the maximum value of each element between the two Matrices.

As seen for the 1st Row and 1st Column, the value of N is higher than that of M so it is taken for the output Matrix while the value in the 1st Row and 2nd Column is higher in M than N do is taken for the Output Matrix and so on and so forth:

Using only 1 input matrix, skipping the second input matrix using `[ ]`

and specifying the Direction as 1 instead finds the Maximum in each Column:

`[Output]=max(M,[],1)`

This results in a Row Vector of maximum values in each Column:

The max function also works this way with only 1 input matrix giving the same result as above:

`[Output]=max(M)`

Using only 1 input matrix, skipping the second input matrix using `[ ]`

and specifying the Direction as 2 instead finds the Maximum in each Row:

`[Output]=max(M,[],2)`

Using only 1 input matrix, skipping the second input matrix using `[ ]`

and specifying the direction as `'all'`

will instead find the single Maximum Value of all the elements in the Matrix outputting a Scalar. Note `'all'`

is a text input and has to be enclosed in `' '`

.

`[Output]=max(M,[],'all')`

`[Output]=max(M(:))`

Recall we can type in:

`max(`

To view the multiple input forms this function accepts. We can select More Help… if we require more information.

# min function

The function

`[Output]=min(M,N,direction)`

Unsurprisingly has an identical form to the `max`

function used above. We can once again compare `M`

and `N`

`M=[1,4,5;7,10,11;13,15,18]`

`N=[2,3,6;8,9,12;14,16,17]`

We go element by element and take the minimum value between the two matrices

`[Output]=min(M,N)`

Using only 1 input matrix, skipping the second input matrix using `[ ]`

and specifying the direction as 1 (Direction 1 finds the Minimum in each Column)

`[Output]=min(M,[],1)`

The min function also works this way with only 1 input matrix:

Using only 1 input matrix, skipping the second input matrix using `[ ]`

and specifying the direction as 2 (Direction 2 finds the Minimum in each Row)

`[Output]=min(M,[],2)`

Using only 1 input matrix, skipping the second input matrix using `[ ]`

and specifying the direction as all (Direction all finds the Minimum in the entire Matrix)

`[Output]=min(M,[],'all')`

`[Output]=min(M(:))`

# sum function

`[Output]=sum(M,direction)`

`M=[1,2,3;4,5,6;7,8,9]`

Inputting the input matrix M and specifying the Direction as 1 (Direction 1 finds the Sum of elements in each Column)

`[Output]=sum(M,1)`

Inputting the input matrix M and specifying the Direction as 2 (Direction 2 finds the Sum of elements in each Row)

`[Output]=sum(M,2)`

Using only 1 input matrix, and specifying the direction as all (Direction all finds the Sum of all elements in the entire Matrix)

`[Output]=sum(M,'all')=sum(M(:))`

We can type in

`sum(`

to get information about the inputs:

# prod function

The function

`[Output]=prod(M,direction)`

has a similar form to the `sum`

function except it calculates the sum of the product opposed to the sum.

`M=[1,2,3;4,5,6;7,8,9]`

Inputting the input matrix M and specifying the Direction as 1 (Direction 1 finds the Product of all elements for each Column)

`[Output]=prod(M,1)`

Inputting the input matrix M and specifying the Direction as 2 (Direction 2 finds the Product of elements in each Row)

`[Output]=prod(M,2)`

Using only 1 input matrix, and specifying the direction as all (Direction all finds the Product of all elements in the entire Matrix)

`[Output]=prod(M,'all')=prod(M(:))`

# mean function

`[Output]=mean(M,direction)`

`M=[1,2,3;4,5,6;7,8,9]`

Inputting the input matrix M and specifying the direction as 1 (Direction 1 finds the Mean in each Column)

`[Output]=mean(M,1)`

Inputting the input matrix M and specifying the direction as 2 (Direction 2 finds the Mean in each Row)

`[Output]=mean(M,2)`

Using only 1 input matrix, and specifying the direction as all (Direction all finds the Mean of the entire Matrix)

`[Output]=mean(M,'all')=mean(M(:))`

# median function

The median finds the middle number in each Column or Row.

`median(M,direction)`

`M=[7,1,4,3;2,9,100,8;10,5,6,11]`

Inputting the input matrix M and specifying the Direction as 1 (Direction 1 finds the Median in each Column)

`[output]=median(M,1)`

`[output]=median(M)`

In this small dataset there is a large outlier:

If we use the mean, it is highly influenced by this outlier and in this case, the median is more representative of the dataset:

Inputting the input matrix M and specifying the Direction as 2 (Direction 2 finds the Median in each Row)

`[output]=median(M,2)`

If we compare this again with the mean, it is highly influenced by the outlier and once again the median is more representative of the dataset:

Using only 1 input matrix, and specifying the direction as all (Direction all finds the Median of the entire Matrix)

`[output]=median(M,'all')=median(M(:))`

# mode function

The mode finds the number that occurs the most.

`[output]=mode(M,direction)`

Inputting the input matrix M and specifying the Direction as 1 (Direction 1 finds the Mode Value in each Column)

`[output]=mode(M,1)`

Inputting the input matrix M and specifying the Direction as 2 (Direction 2 finds the Mode Value in each Row)

`[output]=mode(M,2)`

Using only 1 input matrix, and specifying the direction as all (Direction all finds the Mode of the entire Matrix)

`[output]=mode(M,'all')=mode(M(:))`

# var function

`[Output]=var(M,weight,direction)`

`M=[1,2,3;4,5,6;7,8,9]`

Using 1 input matrix, skipping the second input, the weight using `[ ]`

and specifying the direction as 1 (Direction 1 finds the Variance in each Column)

`[Output]=var(M,[],1)`

Using 1 input matrix, skipping the second input, the weight using `[ ]`

and specifying the direction as 2 (Direction 2 finds the Variance in each Row)

`[Output]=var(M,[],2)`

Using only 1 input matrix, skipping the second input matrix using `[ ]`

and specifying the direction as all (Direction all finds the Variance of the entire Matrix)

`[Output]=var(M,[],'all')`

# std Function

`[Output]=std(M,weight,direction)`

`M=[1,2,3;4,5,6;7,8,9]`

Using 1 input matrix, skipping the second input, the weight using `[ ]`

and specifying the direction as 1 (Direction 1 finds the Standard Deviation in each Column)

`[Output]=std(M,[],1)`

Using 1 input matrix, skipping the second input, the weight using `[ ]`

and specifying the direction as 2 (Direction 2 finds the Standard Deviation in each Row)

`[Output]=std(M,[],2)`

Using only 1 input matrix, skipping the second input matrix using `[ ]`

and specifying the direction as all (Direction all finds the Standard Deviation of the entire Matrix)

`[Output]=std(M,[],'all')`

# cumsum function

Calculates the cumulative sum:

`[Output]=cumsum(M,direction)`

`M=[1,2,3;4,5,6;7,8,9]`

Inputting the input matrix M and specifying the Direction as 1 (Direction 1 finds the Cumulative Sum going down in each Column)

`[Output]=cumsum(M,1)`

Inputting the input matrix M and specifying the Direction as 1 (Direction 2 finds the Cumulative Sum going down in each Row)

`[Output]=cumsum(M,2)`

# cumprod function

Calculates the cumulative product:

`[Output]=cumprod(M,direction)`

`M=[1,2,3;4,5,6;7,8,9]`

Inputting the input matrix M and specifying the Direction as 1 (Direction 1 finds the Cumulative Product going down in each Column)

`[Output]=cumprod(M,1)`

Inputting the input matrix M and specifying the Direction as 2 (Direction 2 finds the Cumulative Sum Value going down in each Row)

`[Output]=cumprod(M,2)`

# diff function

Calculates the difference (approximate derivative) between Matrix elements:

`[Output]=diff(M,order,direction)`

`M=[1,2,3;4,5,6;7,8,9]`

Using 1 input matrix, skipping the second input, the weight using `[ ]`

and specifying the Direction as 1 (Direction 1 finds the Diff going down in each Column)

`[Output]=diff(M,[],1)`

Using 1 input matrix, skipping the second input, the weight using `[ ]`

and specifying the Direction as 2 (Direction 2 finds the Diff going across each Row)

`[Output]=diff(M,[],2)`