Question

(Markov matrix) An n by n matrix is called a positive Markov matrix if each element...

(Markov matrix)

An n by n matrix is called a positive Markov matrix if each element is positive and the sum of the elements in each column is 1. Write the following function to check whether a matrix is a Markov matrix:

def isMarkovMatrix(m):

Write a test program that prompts the user to enter a 3 by 3 matrix of numbers and tests whether it is a Markov matrix. Note that the matrix is entered by rows and the numbers in each row are separated by a space in one line.

Sample Run 1

Enter a 3-by-3 matrix row by row:

0.15 0.875 0.375

0.55 0.005 0.225

0.30 0.12 0.4

It is a Markov matrix

Sample Run 2

Enter a 3-by-3 matrix row by row:

0.95 -0.875 0.375

0.65 0.005 0.225

0.30 0.22 -0.4

It is not a Markov matrix

In Python.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is your code :-

Read comments to understand clearly.

import java.util.Scanner;

public class MarkovMatrix {

static Scanner sc=new Scanner(System.in);           //Scanner object to read values from the user

static String str[];

public static void main(String[] args) {

double mat[][]=new double[3][3];              //3X3 matrix of type double to store values

System.out.println("Enter a 3X3 matrix row by row");     //asking user to enter values into matrix row by row

for(int i=0;i<3;i++)

{

str=sc.nextLine().split(" ");//reading the whole row wntered by user and slitting into an string array by space as delimiter now the str array contains each value of that row column wise such as m[i][j]

for(int j=0;j<3;j++)

{

mat[i][j]=Double.parseDouble(str[j]);     //reading each value from str array and storing it in ith row

}

}

boolean flag=isMarkovMatrix(mat);   //calling function to test whether a matrix is positive markov matrix or not

if(flag)

System.out.println("This is a positive Markov Matrix"); //printing result based on the value returned by function if true yes

else

System.out.println("This is a not positive Markov Matrix"); //printing result based on the value returned by function if false no

}

private static boolean isMarkovMatrix(double[][] m) {

for(int i=0;i<3;i++)

{double count=0;

for(int j=0;j<3;j++)

{

count+=m[j][1];   //calculating column by column count

}

if(count!=1)   //checking column count if any of the rows count is not 1 then returning false

return false;

}

return true; //else returning true

}

}

Output :-

Add a comment
Know the answer?
Add Answer to:
(Markov matrix) An n by n matrix is called a positive Markov matrix if each element...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Part I: Code writing: 1. 3pts (Display matrix of numbers) Write a function that displays an...

    Part I: Code writing: 1. 3pts (Display matrix of numbers) Write a function that displays an n-by-n matrix using the following header: void printMatrix(int n) Each element is between (3,9), which is generated randomly. Write a test program that prompts the user to enter n and displays an n-by-n matrix. Here is a sample run: Enter n: 2 47 96

  • Write a program that reads a matrix from the keyboard and displays the summations of all...

    Write a program that reads a matrix from the keyboard and displays the summations of all its rows on the screen. The size of matrix (i.e. the number of rows and columns) as well as its elements are read from the keyboard. A sample execution of this program is illustrated below: Enter the number of rows of the matrix: 3 Enter the number of columns of the matrix: 4 Enter the element at row 1 and chd umn 1: 1...

  • Python code.. Nine coins are placed in a 3x3 matrix with some face up and some...

    Python code.. Nine coins are placed in a 3x3 matrix with some face up and some face down. You can represent the state of the coins with the values 0 and 1. 000 010 000 etc. Each state can also be represented using a binary number. For example, the preceding matrices correspond to the numbers: 000010000 There are a total of 512 possibilites. So, you can use the decimal numbers 0,1,2,3 etc., and 511 to represent all states of the...

  • Question A matrix of dimensions m × n (an m-by-n matrix) is an ordered collection of m × n elemen...

    Question A matrix of dimensions m × n (an m-by-n matrix) is an ordered collection of m × n elements. which are called eernents (or components). The elements of an (m × n)-dimensional matrix A are denoted as a,, where 1im and1 S, symbolically, written as, A-a(1,1) S (i.j) S(m, ). Written in the familiar notation: 01,1 am Gm,n A3×3matrix The horizontal and vertical lines of entries in a matrix are called rows and columns, respectively A matrix with the...

  • Write C++ programs that create TEN(10) different N*N magic squares. A square matrix is the arrangement...

    Write C++ programs that create TEN(10) different N*N magic squares. A square matrix is the arrangement of the numbers 1, 2, ., N2, in which the sum of rows, columns, and diagonals are the same. The users (i.e., TAs) will specify the size of the square matrix: N. The value N must be an odd number between 3 and 15. Example Run For example, you program is expected to run as the following way. NOTE: We only list 5 magic...

  • 1.Given a positive integer number says n, write a java program to print the first n...

    1.Given a positive integer number says n, write a java program to print the first n squared numbers recursively. Sample run 1: Enter the value of n: 5 ------------------------------------- First 5 square numbers are: 1 4 9 16 25 Sample run 2: Enter the value of n: 10 ------------------------------------- First 10 square numbers are: 1 4 9 16 25 36 49 64 81 100 Sample run 3: Enter the value of n: 12 ------------------------------------- First 12 square numbers are: 1...

  • This needs to be in python, however, I am having trouble with the code. Is there...

    This needs to be in python, however, I am having trouble with the code. Is there any way to use the code that I already have under the main method? If so, what would be the rest of the code to finish it? #Create main method def main(): #Create empty list list=[] #Display to screen print("Please enter a 3 x 4 array:") #Create loop for rows and have each entered number added to list for i in range(3): list.append([int(x) for...

  • Question 4 [35 marks in totalj An n x n matrix A is called a stochastic...

    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...

  • Java Magic Square: A n x n matrix that is filled with the numbers 1, 2,...

    Java Magic Square: A n x n matrix that is filled with the numbers 1, 2, 3,.... n^2 is a magic square if the sum of the elements in each row, in each column, and in the two diagonals is the same value. I need to write an application that gets 16 values as user input, in any order. Store these values in an ArrayList. When the numbers are put into a square, this would be a 4x4 two-dimensional array....

  • Exercise 3: Write a method to add two matrices. The header of the method is: I...

    Exercise 3: Write a method to add two matrices. The header of the method is: I public static double[][] addMatrix(double [ ][] a, double[][] b) In order to be added, the two matrices must have the same dimensions and the same or compatible types of elements. Let e be the resulting matrix. Each element c is c -a, +b For example, for two 3x3 matrices a and b, c is Write a test program that (a) prompts the user to...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT