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
Theory: A vector with nonnegative entries is called a probability vector if the sum of its entries is 1. A square matrix...
Question 3: (a) (4 points) Recall that the trace of a square matrix is the sum of all its entries from the main diagonal. Show that the trace is linear, in the sense that, trace(aX + βΥ) trace(X) + β trace(Y). Let V be the space of all m × n matrices. A function <..) : V × V → R is defined as (A, B) trace(ABT), A, B E V. (a) (4 points) Using the properties of the trace,...
how can i solve the system of these magic matrices using
matlab software ?
Exercice 3. A magic matrix is a square matrix with integer entries in which all the rows, columns and the two diagonals have the same sum. For example, A- 3 5 7 4 9 2 Complete the following magic matrices 17? ?? 3 ? 2 ? 2? ? Do the following steps in each case: 1. Write the system of equations and put it under the...
Question 4 [35 marks in totalj An n x n matrix A is called a stochastic matrix if it! satisfies two conditions: (i) all entries of A are non-negative; and (ii) the sum of entries in each column is one. If the (,) entry of A is denoted by any for ij € {1, 2,...,n}, then A is a stochastic matrix when alij 20 for all i and j and I j = 1 for all j. These matrices are...
In this exercise you will work with LU factorization of an matrix A. Theory: Any matrix A can be reduced to an echelon form by using only row replacement and row interchanging operations. Row interchanging is almost always necessary for a computer realization because it reduces the round off errors in calculations - this strategy in computer calculation is called partial pivoting, which refers to selecting for a pivot the largest by absolute value entry in a column. The MATLAB...
Diagonal Difference HackerRank Pseudocode and C++: Given a square matrix, calculate the absolute difference between the sums of its diagonals. Function Description Complete the diagonalDifference function described below to calculate the absolute difference between diagonal sums. diagonalDifference( integer: a_size_rows, integer: a_size_cols, integer array: arr) Parameters: a_size_rows: number of rows in array a_size_cols: number of columns in array a: array of integers to process Returns: integer value that was calculated Constraints -100 < = elements of the matrix < = 100...
6. + 1/5 points Previous Answers LarLinAlg8 2.5.050. The figure below illustrates an example of a Markov chain with reflecting boundaries. 100% 60% 70% si (S2) (S ) 30% 100% ( SA 40% (a) Explain why it is appropriate to say that this type of Markov chain has reflecting boundaries. When the chain reaches S1 or S4, it is certain in the next step to transition to an adjacent state, S2 or Sz respectively. When the chain reaches S2 or...
Use basic java for this after importing PrintWriter object You will make a simple Magic Square program for this Java Programming Assignment. Carefully read all the instructions before beginning to code. It is also required to turn in your pseudocode or a flowchart along with this program. Here are the instructions: At the beginning of the program, briefly describe to the user what a Magic Square Matrix is (described further on), and then allow them to enter “start” to begin...
IT a) If one row in an echelon form for an augmented matrix is [o 0 5 o 0 b) A vector bis a linear combination of the columns of a matrix A if and only if the c) The solution set of Ai-b is the set of all vectors of the formu +vh d) The columns of a matrix A are linearly independent if the equation A 0has If A and Bare invertible nxn matrices then A- B-'is the...
(1 point) A square matrix is called a permutation matrix if it contains the entry 1 exactly once in each row and in each column with all other entries being 0. All permutation matrices are invertible. Find the inverse of the permutation matrix To 0 1 01 0 0 0 1 A= 0 1 0 0 L1 0 0 0 A- = Preview My Answers Submit Answers
*** Write a function called reverse_diag that creates a square matrix whose elements are 0 except for 1s on the reverse diagonal from top right to bottom left. The reverse diagonal of an nby- n matrix consists of the elements at the following indexes: (1, n), (2, n-1), (3, n-2), … (n, 1). The function takes one positive integer input argument named n, which is the size of the matrix, and returns the matrix itself as an output argument. Note...