Theory: A vector with nonnegative entries is called aprobability vector if the sum of its entries is 1. A square matrixis called right stochastic matrix if its rows are probabilityvectors; a square matrix is called a left stochastic matrix if itscolumns are probability vectors; and a square matrix is called adoubly stochastic matrix if both the rows and the columns areprobability vectors.
**Write a MATLAB function
function [S1,S2,P]=stochastic(A)
which accepts a square matrix A with nonnegative entries as aninput. An output P will be a stochastic matrix, when it can becreated according to the instructions given below. You will alsocalculate and output the row vectors S1=sum(A,1) andS2=transpose(sum(A,2)) in your code, where the entries of thevectors sum(A,1) and sum(A,2) are the sums of the entries of A downeach column and across each row, respectively You may also employthe conditional “if” statements and logical functionall.
**First, the function has to check whether a matrix A containsboth a zero column and a zero row. If yes, the output has to be amessage “A is not stochastic and cannot be scaled to stochastic”.(Meaning: it is neither right- nor left-stochastic and cannot bescaled to either of them.) The output P in this case will be anempty matrix, P= [ ].
**Then, the function checks whether a matrix A is: (1) doublystochastic, (2) only left stochastic, (3) only right stochastic, or(4) neither left nor right stochastic but can be scaled tostochastic.
In the cases (1)-(3), output the corresponding messages thatcomment on the types of the matrices and assign to the output P thematrix A itself.
**When you are working with the case (4), output a message“neither left nor right stochastic but can be scaled tostochastic”. If the vector S1 does not have any zero entry, usethis vector to modify A into a left-stochastic matrix P by scalingeach of its columns by the reciprocal of the corresponding entry ofS1, and output a message that you are scaling A to aleft-stochastic matrix P. If S1 has a zero entry, use the vector S2to modify matrix A into a right-stochastic matrix P by scaling eachof its rows by the reciprocal of the corresponding entry of S2, andoutput a message that you are scaling A to a right-stochasticmatrix P. Output the matrix P.
**Type the function stochastic in your LiveScript.
**Run the function [S1,S2,P]=stochastic(A) on each of thematrices below (display the matrices in your Live Script file):
(a)A=[0.5, 0, 0.5; 0, 0, 1; 0.5, 0, 0.5]
(b)A = transpose(A)
(c)A=[0.5, 0, 0.5; 0, 0, 1; 0, 0, 0.5]
(d)A=transpose(A)
(e)A=[0.5, 0, 0.5; 0, 0.5, 0.5; 0.5, 0.5, 0]
(f)A=magic(3)
(g)A=diag([1,2,3])
(h)A=[0, 0, 0; 0, 0.5, 0.5; 0, 0.5, 0.5]
(k)A=randi(10,5,5);A(:,1)=0;A(1,:)=0
NOTE: Make sure that you will verify that your outputs and themessages match the corresponding definitions of stochasticmatrices. If they don’t, make corrections in your code!