# 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]

$\displaystyle \text{M}=\left[ {\begin{array}{*{20}{c}} 1 & {\mathbf{4}} & 5 \\ 7 & {\mathbf{10}} & {11} \\ {13} & {15} & {\mathbf{18}} \end{array}} \right]$

$\displaystyle \text{N}=\left[ {\begin{array}{*{20}{c}} {\mathbf{2}} & 3 & {\mathbf{6}} \\ {\mathbf{8}} & 9 & {\mathbf{12}} \\ {\mathbf{14}} & {\mathbf{16}} & {17} \end{array}} \right]$

We can use the function:

[Output]=max(M,N)

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

$\displaystyle \text{max(M,N)=}\left[ {\begin{array}{*{20}{c}} {\text{max(1,}}{\mathbf{2)}} & {\text{max(}}{\mathbf{4}}{\text{,3)}} & {\text{max(5,}}{\mathbf{6)}} \\ {\text{max(7,}}{\mathbf{8)}} & {\text{max(}}{\mathbf{10}}{\text{,9)}} & {\text{max(11,}}{\mathbf{12)}} \\ {\text{max(13,}}{\mathbf{14)}} & {\text{max(15,}}{\mathbf{16)}} & {\text{max(}}{\mathbf{18}}{\text{,17)}} \end{array}} \right]=\left[ {\begin{array}{*{20}{c}} 2 & 4 & 6 \\ 8 & {10} & {12} \\ {14} & {16} & {18} \end{array}} \right]$

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)

$\displaystyle \max \left( {\text{M,}\left[ {} \right]\text{,1}} \right)=\left[ {\begin{array}{*{20}{c}} {\max \left( {\left[ {\begin{array}{*{20}{c}} 1 \\ 7 \\ {\mathbf{13}} \end{array}} \right]} \right)} & {\max \left( {\left[ {\begin{array}{*{20}{c}} 4 \\ {10} \\ {\mathbf{15}} \end{array}} \right]} \right)} & {\max \left( {\left[ {\begin{array}{*{20}{c}} 5 \\ {11} \\ {\mathbf{18}} \end{array}} \right]} \right)} \end{array}} \right]$

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

$\displaystyle \max \left( {\text{M,}\left[ {} \right]\text{,1}} \right)=\left[ {\begin{array}{*{20}{c}} {13} & {15} & {18} \end{array}} \right]$

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

[Output]=max(M)

$\displaystyle \max \left( \text{M} \right)=\left[ {\begin{array}{*{20}{c}} {\max \left( {\left[ {\begin{array}{*{20}{c}} 1 \\ 7 \\ {\mathbf{13}} \end{array}} \right]} \right)} & {\max \left( {\left[ {\begin{array}{*{20}{c}} 4 \\ {10} \\ {\mathbf{15}} \end{array}} \right]} \right)} & {\max \left( {\left[ {\begin{array}{*{20}{c}} 5 \\ {11} \\ {\mathbf{18}} \end{array}} \right]} \right)} \end{array}} \right]$

$\displaystyle \max \left( \text{M} \right)=\left[ {\begin{array}{*{20}{c}} {13} & {15} & {18} \end{array}} \right]$

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)

$\displaystyle \max \left( {\text{M,}\left[ {} \right]\text{,2}} \right)=\left[ {\begin{array}{*{20}{c}} {\max \left( {\left[ {\begin{array}{*{20}{c}} 1 & 4 & {\mathbf{5}} \end{array}} \right]} \right)} \\ {\max \left( {\left[ {\begin{array}{*{20}{c}} 7 & {10} & {\mathbf{11}} \end{array}} \right]} \right)} \\ {\max \left( {\left[ {\begin{array}{*{20}{c}} {13} & {15} & {\mathbf{18}} \end{array}} \right]} \right)} \end{array}} \right]$

$\displaystyle \max \left( {\text{M,}\left[ {} \right]\text{,2}} \right)=\left[ {\begin{array}{*{20}{c}} 5 \\ {11} \\ {18} \end{array}} \right]$

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(:))

$\displaystyle \text{max(M,}\left[ {} \right],'\text{all}'\text{)=max(M(:))}=\text{max}\left( {\left[ {\begin{array}{*{20}{c}} 1 \\ 7 \\ {13} \\ 4 \\ {10} \\ {15} \\ 5 \\ {11} \\ {\mathbf{18}} \end{array}} \right]} \right)=18$

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)

$\displaystyle \text{M}=\left[ {\begin{array}{*{20}{c}} {\mathbf{1}} & {4} & {\mathbf{5}} \\ {\mathbf{7}} & {10} & {\mathbf{11}} \\ {\mathbf{13}} & {\mathbf{15}} & {18} \end{array}} \right]$

$\displaystyle \text{N}=\left[ {\begin{array}{*{20}{c}} 2 & {\mathbf{3}} & {6} \\ {8} & {\mathbf{9}} & {12} \\ {14} & {16} & {\mathbf{17}} \end{array}} \right]$

$\displaystyle \text{min(M,N)=}\left[ {\begin{array}{*{20}{c}} {\text{min(}}{\mathbf{1}}{\text{,2)}} & {\text{min(4,}}{\mathbf{3}}{\text{)}} & {\text{min(}}{\mathbf{5}}{\text{,6)}} \\ {\text{min(}}{\mathbf{7}}{\text{,8)}} & {\text{min(10,}}{\mathbf{9}}{\text{)}} & {\text{min(}}{\mathbf{11}}{\text{,12)}} \\ {\text{min(}}{\mathbf{13}}{\text{,14)}} & {\text{min(}}{\mathbf{15}}{\text{,16)}} & {\text{min(18}}{\mathbf{,17}}{\text{)}} \end{array}} \right]=\left[ {\begin{array}{*{20}{c}} 1 & 3 & 5 \\ 7 & 9 & {11} \\ {13} & {15} & {17} \end{array}} \right]$

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)

$\displaystyle \min \left( {\text{M,}\left[ {} \right]\text{,1}} \right)=\left[ {\begin{array}{*{20}{c}} {\min \left( {\left[ {\begin{array}{*{20}{c}} {\mathbf{1}} \\ 7 \\ {13} \end{array}} \right]} \right)} & {\min \left( {\left[ {\begin{array}{*{20}{c}} {\mathbf{4}} \\ {10} \\ {15} \end{array}} \right]} \right)} & {\min \left( {\left[ {\begin{array}{*{20}{c}} {\mathbf{5}} \\ {11} \\ {18} \end{array}} \right]} \right)} \end{array}} \right]$

$\displaystyle \min \left( {\text{M,}\left[ {} \right]\text{,1}} \right)=\left[ {\begin{array}{*{20}{c}} 1 & 4 & 5 \end{array}} \right]$

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

$\displaystyle \min \left( \text{M} \right)=\left[ {\begin{array}{*{20}{c}} 1 & 4 & 5 \end{array}} \right]$

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)

$\displaystyle \min \left( {\text{M,}\left[ {} \right]\text{,2}} \right)=\left[ {\begin{array}{*{20}{c}} {\min \left( {\left[ {\begin{array}{*{20}{c}} {\mathbf{1}} & 4 & 5 \end{array}} \right]} \right)} \\ {\min \left( {\left[ {\begin{array}{*{20}{c}} {\mathbf{7}} & {10} & {11} \end{array}} \right]} \right)} \\ {\min \left( {\left[ {\begin{array}{*{20}{c}} {\mathbf{13}} & {15} & {18} \end{array}} \right]} \right)} \end{array}} \right]$

$\displaystyle \min \left( {\text{M,}\left[ {} \right]\text{,2}} \right)=\left[ {\begin{array}{*{20}{c}} 1 \\ 7 \\ {13} \end{array}} \right]$

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(:))

$\displaystyle \text{min(M,}\left[ {} \right],'\text{all}'\text{)=min(M(:))}=\text{min}\left( {\left[ {\begin{array}{*{20}{c}} 1 \\ 7 \\ {13} \\ 4 \\ {10} \\ {15} \\ 5 \\ {11} \\ {18} \end{array}} \right]} \right)=1$

# sum function

[Output]=sum(M,direction)

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

$\displaystyle \text{M}=\left[ {\begin{array}{*{20}{c}} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \end{array}} \right]$

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)

$\displaystyle \text{sum(M,1)}=\left[ {\begin{array}{*{20}{c}} {\text{sum}\left( {\left[ {\begin{array}{*{20}{c}} 1 \\ 4 \\ 7 \end{array}} \right]} \right)} & {\text{sum}\left( {\left[ {\begin{array}{*{20}{c}} 2 \\ 5 \\ 8 \end{array}} \right]} \right)} & {\text{sum}\left( {\left[ {\begin{array}{*{20}{c}} 3 \\ 6 \\ 9 \end{array}} \right]} \right)} \end{array}} \right]$

$\displaystyle \text{sum(M,1)}=\left[ {\begin{array}{*{20}{c}} {1+4+7} & {2+5+8} & {3+6+9} \end{array}} \right]$

$\displaystyle \text{sum(M,1)}=\left[ {\begin{array}{*{20}{c}} {12} & {15} & {18} \end{array}} \right]$

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)

$\displaystyle \text{sum(M,2)}=\left[ {\begin{array}{*{20}{c}} {\text{sum}\left( {\left[ {\begin{array}{*{20}{c}} 1 & 2 & 3 \end{array}} \right]} \right)} \\ {\text{sum}\left( {\left[ {\begin{array}{*{20}{c}} 4 & 5 & 6 \end{array}} \right]} \right)} \\ {\text{sum}\left( {\left[ {\begin{array}{*{20}{c}} 7 & 8 & 9 \end{array}} \right]} \right)} \end{array}} \right]$

$\displaystyle \text{sum(M,2)}=\left[ {\begin{array}{*{20}{c}} {1+2+3} \\ {4+5+6} \\ {7+8+9} \end{array}} \right]$

$\displaystyle \text{sum(M,2)}=\left[ {\begin{array}{*{20}{c}} 6 \\ {15} \\ {24} \end{array}} \right]$

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(:))

$\displaystyle \text{sum(M,}'\text{all}'\text{)=sum(M(:))}=\text{sum}\left( {\left[ {\begin{array}{*{20}{c}} 1 \\ 4 \\ 7 \\ 2 \\ 5 \\ 8 \\ 3 \\ 6 \\ 9 \end{array}} \right]} \right)=45$

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]

$\displaystyle \text{M}=\left[ {\begin{array}{*{20}{c}} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \end{array}} \right]$

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)

$\displaystyle \text{prod(M,1)}=\left[ {\begin{array}{*{20}{c}} {\text{prod}\left( {\left[ {\begin{array}{*{20}{c}} 1 \\ 4 \\ 7 \end{array}} \right]} \right)} & {\text{prod}\left( {\left[ {\begin{array}{*{20}{c}} 2 \\ 5 \\ 8 \end{array}} \right]} \right)} & {\text{prod}\left( {\left[ {\begin{array}{*{20}{c}} 3 \\ 6 \\ 9 \end{array}} \right]} \right)} \end{array}} \right]$

$\displaystyle \text{prod(M,1)}=\left[ {\begin{array}{*{20}{c}} {1*4*7} & {2*5*8} & {3*6*9} \end{array}} \right]$

$\displaystyle \text{prod(M,1)}=\left[ {\begin{array}{*{20}{c}} {28} & {80} & {162} \end{array}} \right]$

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)

$\displaystyle \text{prod(M,2)}=\left[ {\begin{array}{*{20}{c}} {\text{prod}\left( {\left[ {\begin{array}{*{20}{c}} 1 & 2 & 3 \end{array}} \right]} \right)} \\ {\text{prod}\left( {\left[ {\begin{array}{*{20}{c}} 4 & 5 & 6 \end{array}} \right]} \right)} \\ {\text{prod}\left( {\left[ {\begin{array}{*{20}{c}} 7 & 8 & 9 \end{array}} \right]} \right)} \end{array}} \right]$

$\displaystyle \text{prod(M,2)}=\left[ {\begin{array}{*{20}{c}} {1*2*3} \\ {4*5*6} \\ {7*8*9} \end{array}} \right]$

$\displaystyle \text{prod(M,2)}=\left[ {\begin{array}{*{20}{c}} 6 \\ {120} \\ {504} \end{array}} \right]$

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(:))

$\displaystyle \text{prod(M,}'\text{all}'\text{)=prod(M(:))}=\text{prod}\left( {\left[ {\begin{array}{*{20}{c}} 1 \\ 4 \\ 7 \\ 2 \\ 5 \\ 8 \\ 3 \\ 6 \\ 9 \end{array}} \right]} \right)=362880$

# mean function

$\displaystyle {{\mu }_{x}}=\text{mean}=\sum\nolimits_{{i=1}}^{m}{{{{x}_{i}}}}$

[Output]=mean(M,direction)

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

$\displaystyle \text{M}=\left[ {\begin{array}{*{20}{c}} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \end{array}} \right]$

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

[Output]=mean(M,1)

$\displaystyle \text{mean}\left( {\text{M},1} \right)=\left[ {\begin{array}{*{20}{c}} {\text{mean}\left( {\begin{array}{*{20}{c}} 1 \\ 4 \\ 7 \end{array}} \right)} & {\text{mean}\left( {\begin{array}{*{20}{c}} 2 \\ 5 \\ 8 \end{array}} \right)} & {\text{mean}\left( {\begin{array}{*{20}{c}} 3 \\ 6 \\ 9 \end{array}} \right)} \end{array}} \right]=\left[ {\begin{array}{*{20}{c}} 4 & 5 & 6 \end{array}} \right]$

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

[Output]=mean(M,2)

$\displaystyle \text{mean(M},2\text{)}=\left[ {\begin{array}{*{20}{c}} {\text{mean}\left( {\begin{array}{*{20}{c}} 1 & 2 & 3 \end{array}} \right)} \\ {\text{mean}\left( {\begin{array}{*{20}{c}} 4 & 5 & 6 \end{array}} \right)} \\ {\text{mean}\left( {\begin{array}{*{20}{c}} 7 & 8 & 9 \end{array}} \right)} \end{array}} \right]=\left[ {\begin{array}{*{20}{c}} 2 \\ 5 \\ 8 \end{array}} \right]$

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(:))

$\displaystyle \text{mean(M,}'\text{all}'\text{)=mean(M(:))}=\text{mean}\left( {\left[ {\begin{array}{*{20}{c}} 1 \\ 4 \\ 7 \\ 2 \\ 5 \\ 8 \\ 3 \\ 6 \\ 9 \end{array}} \right]} \right)=5$

# 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]

$\displaystyle \text{M}=\left[ {\begin{array}{*{20}{c}} 7 & 1 & 4 & 3 \\ 2 & 9 & {100} & 8 \\ {10} & 5 & 6 & {11} \end{array}} \right]$

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

[output]=median(M,1)

$\displaystyle \text{median}\left( {\text{M,1}} \right)=$

$\displaystyle \left[ {\begin{array}{*{20}{c}} {\text{median}\left( {\left[ {\begin{array}{*{20}{c}} 7 \\ 2 \\ {10} \end{array}} \right]} \right)} & {\text{median}\left( {\left[ {\begin{array}{*{20}{c}} 1 \\ 9 \\ 5 \end{array}} \right]} \right)} & {\text{median}\left( {\left[ {\begin{array}{*{20}{c}} 4 \\ {100} \\ 6 \end{array}} \right]} \right)} & {\text{median}\left( {\left[ {\begin{array}{*{20}{c}} 3 \\ 8 \\ {11} \end{array}} \right]} \right)} \end{array}} \right]$

$\displaystyle \text{median}\left( {\text{M,1}} \right)=\left[ {\begin{array}{*{20}{c}} 7 & 5 & 6 & 8 \end{array}} \right]$

[output]=median(M)

$\displaystyle \text{median}\left( \text{M} \right)=\left[ {\begin{array}{*{20}{c}} 7 & 5 & 6 & 8 \end{array}} \right]$

In this small dataset there is a large outlier:

$\displaystyle \text{M}=\left[ {\begin{array}{*{20}{c}} 7 & 1 & 4 & 3 \\ 2 & 9 & {\mathbf{100}} & 8 \\ {10} & 5 & 6 & {11} \end{array}} \right]$

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

$\displaystyle \text{mean}\left( {\text{M,1}} \right)=\left[ {\begin{array}{*{20}{c}} {6.3333} & {5.0000} & {36.6667} & {7.3333} \end{array}} \right]$

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

[output]=median(M,2)

$\displaystyle \text{median}\left( {\text{M,2}} \right)=\left[ {\begin{array}{*{20}{c}} {\text{median}\left( {\left[ {\begin{array}{*{20}{c}} 7 & 1 & 4 & 3 \end{array}} \right]} \right)} \\ {\text{median}\left( {\left[ {\begin{array}{*{20}{c}} 2 & 9 & {100} & 8 \end{array}} \right]} \right)} \\ {\text{median}\left( {\left[ {\begin{array}{*{20}{c}} {10} & 5 & 6 & {11} \end{array}} \right]} \right)} \end{array}} \right]$

$\displaystyle \text{median}\left( {\text{M,2}} \right)=\left[ {\begin{array}{*{20}{c}} {3.5} \\ {8.5} \\ 8 \end{array}} \right]$

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:

$\displaystyle \text{mean}\left( {\text{M,2}} \right)=\left[ {\begin{array}{*{20}{c}} {3.75} \\ {29.75} \\ 8 \end{array}} \right]$

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(:))

$\displaystyle \text{median(M, }\!\!'\!\!\text{ all }\!\!'\!\!\text{ )}=\text{median(M(:))}=\text{median}\left( {\left[ {\begin{array}{*{20}{c}} 7 \\ 2 \\ {10} \\ 1 \\ 9 \\ 5 \\ 4 \\ {100} \\ 6 \\ 3 \\ 8 \\ {11} \end{array}} \right]} \right)=6.5$

# mode function

The mode finds the number that occurs the most.

[output]=mode(M,direction)

$\displaystyle \text{M}=\left[ {\begin{array}{*{20}{c}} 1 & 2 & 9 & 9 \\ 1 & 9 & {14} & 9 \\ {11} & 9 & {14} & {14} \end{array}} \right]$

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

[output]=mode(M,1)

$\displaystyle \text{mode(M,1)=}\left[ {\begin{array}{*{20}{c}} {\text{mode}\left( {\left[ {\begin{array}{*{20}{c}} {\mathbf{1}} \\ {\mathbf{1}} \\ {11} \end{array}} \right]} \right)} & {\text{mode}\left( {\left[ {\begin{array}{*{20}{c}} 2 \\ {\mathbf{9}} \\ {\mathbf{9}} \end{array}} \right]} \right)} & {\text{mode}\left( {\left[ {\begin{array}{*{20}{c}} 9 \\ {\mathbf{14}} \\ {\mathbf{14}} \end{array}} \right]} \right)} & {\text{mode}\left( {\left[ {\begin{array}{*{20}{c}} {\mathbf{9}} \\ {\mathbf{9}} \\ {14} \end{array}} \right]} \right)} \end{array}} \right]$

$\displaystyle \text{mode(M,1)=}\left[ {\begin{array}{*{20}{c}} 1 & 9 & {14} & 9 \end{array}} \right]$

$\displaystyle \text{mode(M)=}\left[ {\begin{array}{*{20}{c}} 1 & 9 & {14} & 9 \end{array}} \right]$

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

[output]=mode(M,2)

$\displaystyle \text{mode(M,2)=}\left[ {\begin{array}{*{20}{c}} {\text{mode}\left( {\left[ {\begin{array}{*{20}{c}} 1 & 2 & {\mathbf{9}} & {\mathbf{9}} \end{array}} \right]} \right)} \\ {\text{mode}\left( {\left[ {\begin{array}{*{20}{c}} 1 & {\mathbf{9}} & {14} & {\mathbf{9}} \end{array}} \right]} \right)} \\ {\text{mode}\left( {\left[ {\begin{array}{*{20}{c}} {11} & 9 & {\mathbf{14}} & {\mathbf{14}} \end{array}} \right]} \right)} \end{array}} \right]$

$\displaystyle \text{mode(M,2)=}\left[ {\begin{array}{*{20}{c}} 9 \\ 9 \\ {14} \end{array}} \right]$

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(:))

$\displaystyle \text{mode(M(:))}=\text{mode(M, }\!\!'\!\!\text{ all }\!\!'\!\!\text{ )}=\left[ {\begin{array}{*{20}{c}} 1 \\ 1 \\ {11} \\ 2 \\ {\mathbf{9}} \\ {\mathbf{9}} \\ {\mathbf{9}} \\ {14} \\ {14} \\ {\mathbf{9}} \\ {\mathbf{9}} \\ {14} \end{array}} \right]=9$

# var function

$\displaystyle \text{var=}\frac{{\sum\limits_{{i=1}}^{m}{{{{{\left( {x-{{\mu }_{x}}} \right)}}^{2}}}}}}{{m-1}}=\frac{{\sum\limits_{{i=1}}^{m}{{{{{\left( {x-{{x}_{{\text{mean}}}}} \right)}}^{2}}}}}}{{m-1}}$

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

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

$\displaystyle \text{M}=\left[ {\begin{array}{*{20}{c}} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \end{array}} \right]$

$\displaystyle \text{mean}\left( {\text{M,1}} \right)=\left[ {\begin{array}{*{20}{c}} 4 & 5 & 6 \end{array}} \right]$

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)

$\displaystyle \text{var}\left( {\text{M,}\left[ {} \right]\text{,1}} \right)=\left[ {\begin{array}{*{20}{c}} {\text{std}\left( {\begin{array}{*{20}{c}} 1 \\ 4 \\ 7 \end{array}} \right)} & {\text{std}\left( {\begin{array}{*{20}{c}} 2 \\ 5 \\ 8 \end{array}} \right)} & {\text{std}\left( {\begin{array}{*{20}{c}} 3 \\ 6 \\ 9 \end{array}} \right)} \end{array}} \right]$

$\displaystyle \text{var}\left( {\text{M,}\left[ {} \right]\text{,1}} \right)=\left[ {\begin{array}{*{20}{c}} {\frac{{{{{\left( {1-4} \right)}}^{2}}+{{{\left( {4-4} \right)}}^{2}}+{{{\left( {7-4} \right)}}^{2}}}}{{3-1}}} & {\frac{{{{{\left( {2-5} \right)}}^{2}}+{{{\left( {5-5} \right)}}^{2}}+{{{\left( {8-5} \right)}}^{2}}}}{{3-1}}} & {\frac{{{{{\left( {3-6} \right)}}^{2}}+{{{\left( {6-6} \right)}}^{2}}+{{{\left( {9-6} \right)}}^{2}}}}{{3-1}}} \end{array}} \right]$

$\displaystyle \text{var}\left( {\text{M,}\left[ {} \right]\text{,1}} \right)=\left[ {\begin{array}{*{20}{c}} 9 & 9 & 9 \end{array}} \right]$

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)

$\displaystyle \text{mean}\left( {\text{M,2}} \right)=\left[ {\begin{array}{*{20}{c}} 2 \\ 5 \\ 8 \end{array}} \right]$

$\displaystyle \text{var}\left( {\text{M,}\left[ {} \right]\text{,2}} \right)=\left[ {\begin{array}{*{20}{c}} {\text{var}\left( {\begin{array}{*{20}{c}} 1 & 2 & 3 \end{array}} \right)} \\ {\text{var}\left( {\begin{array}{*{20}{c}} 4 & 5 & 6 \end{array}} \right)} \\ {\text{var}\left( {\begin{array}{*{20}{c}} 7 & 8 & 9 \end{array}} \right)} \end{array}} \right]$

$\displaystyle \text{var}\left( {\text{M,}\left[ {} \right]\text{,2}} \right)=\left[ {\begin{array}{*{20}{c}} {\frac{{{{{(1-2)}}^{2}}+{{{\left( {2-2} \right)}}^{2}}+{{{\left( {3-2} \right)}}^{2}}}}{{3-1}}} \\ {\frac{{{{{(4-5)}}^{2}}+{{{\left( {5-5} \right)}}^{2}}+{{{\left( {6-5} \right)}}^{2}}}}{{3-1}}} \\ {\frac{{{{{(7-8)}}^{2}}+{{{\left( {8-8} \right)}}^{2}}+{{{\left( {9-8} \right)}}^{2}}}}{{3-1}}} \end{array}} \right]$

$\displaystyle \text{var}\left( {\text{M,}\left[ {} \right]\text{,2}} \right)=\left[ {\begin{array}{*{20}{c}} 1 \\ 1 \\ 1 \end{array}} \right]$

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')

$\displaystyle \text{var}\left( {\text{M,}\left[ {} \right],\text{ }\!\!'\!\!\text{ all }\!\!'\!\!\text{ }} \right)=\text{var}\left( {\begin{array}{*{20}{c}} 1 \\ 4 \\ 7 \\ 2 \\ 5 \\ 8 \\ 3 \\ 6 \\ 9 \end{array}} \right)$

# std Function

$\displaystyle \text{std}=\sqrt{{\frac{{{{{\sum\limits_{{i=1}}^{m}{{\left( {x-{{\mu }_{x}}} \right)}}}}^{2}}}}{{m-1}}}}=\sqrt{{\frac{{{{{\sum\limits_{{i=1}}^{m}{{\left( {x-{{x}_{{\text{mean}}}}} \right)}}}}^{2}}}}{{m-1}}}}$

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

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

$\displaystyle \text{M}=\left[ {\begin{array}{*{20}{c}} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \end{array}} \right]$

$\displaystyle \text{mean}\left( {\text{M,1}} \right)=\left[ {\begin{array}{*{20}{c}} 4 & 5 & 6 \end{array}} \right]$

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)

$\displaystyle \text{std}\left( {\text{M},\left[ {} \right],1} \right)=\left[ {\begin{array}{*{20}{c}} {\text{var}\left( {\begin{array}{*{20}{c}} 1 \\ 4 \\ 7 \end{array}} \right)} & {\text{var}\left( {\begin{array}{*{20}{c}} 2 \\ 5 \\ 8 \end{array}} \right)} & {\text{var}\left( {\begin{array}{*{20}{c}} 3 \\ 6 \\ 9 \end{array}} \right)} \end{array}} \right]$

$\displaystyle \text{std}\left( {\text{M},\left[ {} \right],1} \right)=\left[ {\begin{array}{*{20}{c}} {\sqrt{{\frac{{{{{\left( {1-4} \right)}}^{2}}+{{{\left( {4-4} \right)}}^{2}}+{{{\left( {7-4} \right)}}^{2}}}}{{3-1}}}}} & {\sqrt{{\frac{{{{{\left( {2-5} \right)}}^{2}}+{{{\left( {5-5} \right)}}^{2}}+{{{\left( {8-5} \right)}}^{2}}}}{{3-1}}}}} & {\sqrt{{\frac{{{{{\left( {3-6} \right)}}^{2}}+{{{\left( {6-6} \right)}}^{2}}+{{{\left( {9-6} \right)}}^{2}}}}{{3-1}}}}} \end{array}} \right]$

$\displaystyle \text{std}\left( {\text{M},\left[ {} \right],1} \right)=\left[ {\begin{array}{*{20}{c}} 3 & 3 & 3 \end{array}} \right]$

$\displaystyle \text{std}\left( {\text{M},\left[ {} \right],1} \right)=\sqrt{{\text{var}\left( {\text{M},\left[ {} \right],1} \right)}}$

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)

$\displaystyle \text{std}\left( {\text{M},\left[ {} \right],2} \right)=\left[ {\begin{array}{*{20}{c}} {\text{std}\left( {\begin{array}{*{20}{c}} 1 & 2 & 3 \end{array}} \right)} \\ {\text{std}\left( {\begin{array}{*{20}{c}} 4 & 5 & 6 \end{array}} \right)} \\ {\text{std}\left( {\begin{array}{*{20}{c}} 7 & 8 & 9 \end{array}} \right)} \end{array}} \right]$

$\displaystyle \text{std}\left( {\text{M},\left[ {} \right],2} \right)=\left[ {\begin{array}{*{20}{c}} {\sqrt{{\frac{{{{{\left( {1-2} \right)}}^{2}}+{{{\left( {2-2} \right)}}^{2}}+{{{\left( {3-2} \right)}}^{2}}}}{3}}}} \\ {\sqrt{{\frac{{{{{\left( {4-5} \right)}}^{2}}+{{{\left( {5-5} \right)}}^{2}}+{{{\left( {6-5} \right)}}^{2}}}}{3}}}} \\ {\sqrt{{\frac{{{{{\left( {7-8} \right)}}^{2}}+{{{\left( {8-8} \right)}}^{2}}+{{{\left( {9-8} \right)}}^{2}}}}{3}}}} \end{array}} \right]$

$\displaystyle \text{std}\left( {\text{M},\left[ {} \right],2} \right)=\left[ {\begin{array}{*{20}{c}} 1 \\ 1 \\ 1 \end{array}} \right]$

$\displaystyle \text{std}\left( {\text{M},\left[ {} \right],2} \right)=\sqrt{{\text{var}\left( {\text{M},\left[ {} \right],1} \right)}}$

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')

$\displaystyle \text{std}\left( {\text{M},\left[ {} \right],'\text{all}'} \right)=\text{std}\left( {\text{M(:)}} \right)=\text{std}\left( {\left[ {\begin{array}{*{20}{c}} 1 \\ 4 \\ 7 \\ 2 \\ 5 \\ 8 \\ 3 \\ 6 \\ 9 \end{array}} \right]} \right)$

$\displaystyle \text{std}\left( {\text{M},\left[ {} \right],'\text{all}'} \right)=\sqrt{{\text{var}\left( {\text{M},\left[ {} \right],'\text{all }\!\!'\!\!\text{ }} \right)}}$

# cumsum function

Calculates the cumulative sum:

$\displaystyle \text{V=}\left[ {\begin{array}{*{20}{c}} \text{A} \\ \text{B} \\ \text{C} \end{array}} \right]$

$\displaystyle \text{cumsum}\left( \text{V} \right)=\left[ {\begin{array}{*{20}{c}} \text{A} \\ {\text{A+B}} \\ {\text{A+B+C}} \end{array}} \right]$

[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)

$\displaystyle \text{cumsum(M,1)}=\left[ {\text{cumsum}\begin{array}{*{20}{c}} {\left[ {\begin{array}{*{20}{c}} 1 \\ 4 \\ 7 \end{array}} \right]} & {\text{cumsum}\left[ {\begin{array}{*{20}{c}} 2 \\ 5 \\ 8 \end{array}} \right]} & {\text{cumsum}\left[ {\begin{array}{*{20}{c}} 3 \\ 6 \\ 9 \end{array}} \right]} \end{array}} \right]$

$\displaystyle \text{cumsum(M,1)}=\left[ {\begin{array}{*{20}{c}} 1 & 2 & 3 \\ {1+4} & {2+5} & {3+6} \\ {1+4+7} & {2+5+8} & {3+6+9} \end{array}} \right]$

$\displaystyle \text{cumsum(M,1)}=\left[ {\begin{array}{*{20}{c}} 1 & 2 & 3 \\ 5 & 7 & 9 \\ {12} & {15} & {18} \end{array}} \right]$

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)

$\displaystyle \text{cumsum(M,2)}=\left[ {\begin{array}{*{20}{c}} {\text{cumsum}\left[ {\begin{array}{*{20}{c}} 1 & 2 & 3 \end{array}} \right]} \\ {\text{cumsum}\left[ {\begin{array}{*{20}{c}} 4 & 5 & 6 \end{array}} \right]} \\ {\text{cumsum}\left[ {\begin{array}{*{20}{c}} 7 & 8 & 9 \end{array}} \right]} \end{array}} \right]$

$\displaystyle \text{cumsum(M,2)}=\left[ {\begin{array}{*{20}{c}} 1 & {1+2} & {1+2+3} \\ 4 & {4+5} & {4+5+6} \\ 7 & {7+8} & {7+8+9} \end{array}} \right]$

$\displaystyle \text{cumsum(M,2)}=\left[ {\begin{array}{*{20}{c}} 1 & 3 & 6 \\ 4 & 9 & {15} \\ 7 & {15} & {24} \end{array}} \right]$

# cumprod function

Calculates the cumulative product:

$\displaystyle \text{V=}\left[ {\begin{array}{*{20}{c}} \text{A} \\ \text{B} \\ \text{C} \end{array}} \right]$

$\displaystyle \text{cumprod(V)}=\left[ {\begin{array}{*{20}{c}} \text{A} \\ {\text{A}*\text{B}} \\ {\text{A}*\text{B}*\text{C}} \end{array}} \right]$

[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)

$\displaystyle \text{cumprod(M,1)}=\left[ {\text{cumprod}\begin{array}{*{20}{c}} {\left[ {\begin{array}{*{20}{c}} 1 \\ 4 \\ 7 \end{array}} \right]} & {\text{cumprod}\left[ {\begin{array}{*{20}{c}} 2 \\ 5 \\ 8 \end{array}} \right]} & {\text{cumprod}\left[ {\begin{array}{*{20}{c}} 3 \\ 6 \\ 9 \end{array}} \right]} \end{array}} \right]$

$\displaystyle \text{cumprod(M,1)}=\left[ {\begin{array}{*{20}{c}} 1 & 2 & 3 \\ {1*4} & {2*5} & {3*6} \\ {1*4*7} & {2*5*8} & {3*6*9} \end{array}} \right]$

$\displaystyle \text{cumprod(M,1)}=\left[ {\begin{array}{*{20}{c}} 1 & 2 & 3 \\ 4 & {10} & {18} \\ {28} & {80} & {162} \end{array}} \right]$

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)

$\displaystyle \text{cumprod(M,2)}=\left[ {\begin{array}{*{20}{c}} {\text{cumprod}\left[ {\begin{array}{*{20}{c}} 1 & 2 & 3 \end{array}} \right]} \\ {\text{cumprod}\left[ {\begin{array}{*{20}{c}} 4 & 5 & 6 \end{array}} \right]} \\ {\text{cumprod}\left[ {\begin{array}{*{20}{c}} 7 & 8 & 9 \end{array}} \right]} \end{array}} \right]$

$\displaystyle \text{cumprod(M,2)}=\left[ {\begin{array}{*{20}{c}} 1 & {1*2} & {1*2*3} \\ 4 & {4*5} & {4*5*6} \\ 7 & {7*8} & {7*8*9} \end{array}} \right]$

$\displaystyle \text{cumprod(M,2)}=\left[ {\begin{array}{*{20}{c}} 1 & 2 & 6 \\ 4 & {20} & {120} \\ 7 & {56} & {504} \end{array}} \right]$

# diff function

Calculates the difference (approximate derivative) between Matrix elements:

$\displaystyle \text{V}=\left[ {\begin{array}{*{20}{c}} \text{A} \\ \text{B} \\ \text{C} \end{array}} \right]$

$\displaystyle \text{diff(V)}=\left[ {\begin{array}{*{20}{c}} {\text{B}-\text{A}} \\ {\text{C}-\text{B}} \end{array}} \right]$

[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)

$\displaystyle \text{diff(M,}\left[ {} \right]\text{,1)}=\left[ {\begin{array}{*{20}{c}} {\text{diff}\left( {\left[ {\begin{array}{*{20}{c}} 1 \\ 4 \\ 7 \end{array}} \right]} \right)} & {\text{diff}\left( {\left[ {\begin{array}{*{20}{c}} 2 \\ 5 \\ 8 \end{array}} \right]} \right)} & {\text{diff}\left( {\left[ {\begin{array}{*{20}{c}} 3 \\ 6 \\ 9 \end{array}} \right]} \right)} \end{array}} \right]$

$\displaystyle \text{diff(M,}\left[ {} \right]\text{,1)}=\left[ {\begin{array}{*{20}{c}} {4-1} & {5-2} & {6-3} \\ {7-4} & {8-5} & {9-6} \end{array}} \right]$

$\displaystyle \text{diff(M,}\left[ {} \right]\text{,1)}=\left[ {\begin{array}{*{20}{c}} 3 & 3 & 3 \\ 3 & 3 & 3 \end{array}} \right]$

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)

$\displaystyle \text{diff(M,}\left[ {} \right]\text{,2)}=\left[ {\begin{array}{*{20}{c}} {\text{diff}\left( {\left[ {\begin{array}{*{20}{c}} 1 & 2 & 3 \end{array}} \right]} \right)} \\ {\text{diff}\left( {\left[ {\begin{array}{*{20}{c}} 4 & 5 & 6 \end{array}} \right]} \right)} \\ {\text{diff}\left( {\left[ {\begin{array}{*{20}{c}} 7 & 8 & 9 \end{array}} \right]} \right)} \end{array}} \right]$

$\displaystyle \text{diff(M,}\left[ {} \right]\text{,2)}=\left[ {\begin{array}{*{20}{c}} {2-1} & {3-2} \\ {5-4} & {6-5} \\ {8-7} & {9-8} \end{array}} \right]$

$\displaystyle \text{diff(M,}\left[ {} \right]\text{,2)}=\left[ {\begin{array}{*{20}{c}} 1 & 1 \\ 1 & 1 \\ 1 & 1 \end{array}} \right]$