In: Advanced Math
Theory: A vector with nonnegative entries is called a probability vector if the sum of its entries is 1. A square matrix is called right stochastic matrix if its rows are probability vectors; a square matrix is called a left stochastic matrix if its columns are probability vectors; and a square matrix is called a doubly stochastic matrix if both the rows and the columns are probability vectors.
**Write a MATLAB function
function [S1,S2,P]=stochastic(A)
which accepts a square matrix A with nonnegative entries as an input. An output P will be a stochastic matrix, when it can be created according to the instructions given below. You will also calculate and output the row vectors S1=sum(A,1) and S2=transpose(sum(A,2)) in your code, where the entries of the vectors sum(A,1) and sum(A,2) are the sums of the entries of A down each column and across each row, respectively You may also employ the conditional “if” statements and logical function all.
**First, the function has to check whether a matrix A contains both a zero column and a zero row. If yes, the output has to be a message “A is not stochastic and cannot be scaled to stochastic”. (Meaning: it is neither right- nor left-stochastic and cannot be scaled to either of them.) The output P in this case will be an empty matrix, P= [ ].
**Then, the function checks whether a matrix A is: (1) doubly stochastic, (2) only left stochastic, (3) only right stochastic, or (4) neither left nor right stochastic but can be scaled to stochastic.
In the cases (1)-(3), output the corresponding messages that comment on the types of the matrices and assign to the output P the matrix A itself.
**When you are working with the case (4), output a message “neither left nor right stochastic but can be scaled to stochastic”. If the vector S1 does not have any zero entry, use this vector to modify A into a left-stochastic matrix P by scaling each of its columns by the reciprocal of the corresponding entry of S1, and output a message that you are scaling A to a left-stochastic matrix P. If S1 has a zero entry, use the vector S2 to modify matrix A into a right-stochastic matrix P by scaling each of its rows by the reciprocal of the corresponding entry of S2, and output a message that you are scaling A to a right-stochastic matrix P. Output the matrix P.
**Type the function stochastic in your Live Script.
**Run the function [S1,S2,P]=stochastic(A) on each of the matrices 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 the messages match the corresponding definitions of stochastic matrices. If they don’t, make corrections in your code!
MATLAB FUNCTION:
function P=stochastic(A)
S1=sum(A,1);
S2=(sum(A,2))';
if ~all(S1) && ~all(S2)
fprintf('A is not stochastic and cannot be scaled to either of them
')
P=[];
elseif sum(S1==1)==length(S1) &&
sum(S2==1)==length(1)
fprintf('A is a doubly stochastic matrix ')
P=A;
elseif sum(S2==1)==length(S1)
fprintf('A is a right stochastic matrix ')
P=A;
elseif sum(S1==1)==length(S2)
fprintf('A is a left stochastic matrix ')
P=A;
else
fprintf('Neither left nor right stochastic but can be scaled to
stochastic. ')
if all(S1)
A=A./S1;
P=A;
else
A=A./(S2)';
P=A;
end
end