Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

**Write a MATLAB function that begins with function [S1,S2,L,R]=stochastic(A) L=[]; R=[]; It accepts as the input a square matrix A with nonnegative entries. Outputs L

**Write a MATLAB function that begins with function [S1,S2,L,R]=stochastic(A) L=[]; R=[]; It accepts as the input a square matrix A with nonnegative entries. Outputs L and R will be either empty matrices or the left stochastic and right stochastic matrices generated according to the instructions given below. The function also outputs and displays the vectors S1=sum(A)and S2=sum(A,2) with the corresponding messages: fprintf('the vector of sums down each column is ') S1=sum(A) fprintf('the vector of sums across each row is ') S2=sum(A,2) You will need to use a conditional if statement to proceed with the tasks outlined below. Hint: You may also find it helpful to employ in this part MATLAB logical command all. **First, your function has to check whether A contains both a zero column and a zero row. If yes, output a message A is neither left nor right stochastic and cannot be scaled to either of them. The empty outputs assigned previously to L and R will stay. 10 **Then, we will check if A is only left stochastic, or only right stochastic, or both left and right stochastic, that is, doubly stochastic. You will need to program each of the three cases, output a corresponding message on the type of the matrix, and make the assignments listed below: (1) If A is doubly stochastic, we assign: L=A;R=A); (2) If A is only left stochastic, we assign (R will stay empty): L=A; (3) If A is only right stochastic, we assign (L will stay empty): R=A; Here is an example of the output for case (3): disp('A is only left stochastic') L=A; **Finally, we consider a possibility that A is neither left nor right stochastic but can be scaled to a left stochastic and/or to a right stochastic matrix. In this case, we output a message 'A is neither left nor right stochastic but can be scaled to stochastic' and proceed with the scaling* in the ways outlined below: Part 1 If neither S1 nor S2 has a zero entry, we are scaling* A to the left stochastic matrix L and the right stochastic matrix R. Then, we check if the matrices L and R are equal (use the function closetozeroroundoff with p=7). If it is the case, then A has been scaled to a doubly stochastic matrix and we output one of the matrices, say L, with the message: disp('A has been scaled to a doubly stochastic matrix:') disp(L) If L and R are not equal, display each of them separately with a corresponding message: disp('A is scaled to a left stochastic matrix:') L disp('and A is scaled to a right stochastic matrix:') R Part 2 If S1 does not have a zero entry but S2 does, we can only scale A to the left stochastic matrix L (R stays empty). An output in this case will be: disp('A is not stochastic but can be scaled to left stochastic only:') L Part 3 And, if S2 does not have a zero entry but S1 does, we can only scale A to the right stochastic matrix R (L stays empty). An output in this case will be: disp('A is not stochastic but can be scaled to right stochastic only:') R *Scaling: To scale A to a left stochastic matrix L, we use vector S1 and multiply each column of A by the reciprocal of the corresponding entry of S1. To scale A to a right stochastic matrix R, we use the vector S2 and multiply each row of A by the reciprocal of the corresponding entry of S2.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Big Data, Mining, And Analytics Components Of Strategic Decision Making

Authors: Stephan Kudyba

1st Edition

1466568704, 9781466568709

More Books

Students also viewed these Databases questions