Posted on by Kalkicode
Code Matrix

Determine whether matrix is Markov matrix or not

Here given code implementation process.

/*
  C Program 
  Determine whether matrix is Markov matrix or not
*/
#include<stdio.h>
#define ROW 3
#define COL 6

//Check that matrix is form of markov matrix or not
void markov_matrix(double matrix[ROW][COL])
{
  int status=1;
  double sum=0;
 
  for (int i = 0; i < ROW; ++i)
  {
    sum=0;
    //Calculate row sum
    for (int j = 0; j < COL; ++j)
    {
      sum+=matrix[i][j];
    }
    if(sum!=1.0f)
    {
      //When sum is not equal to 1
      status=0;
      break;
    }
    
  }
  if(status==1)
  {
    printf("This is a Markov Matrix\n");
  }
  else
  {
     printf("This is not a Markov Matrix\n");
  }
 
}
int main(){

  double matrix[ROW][COL]=  
  { 
    { -1  , -1, 1  , 3, -1, 0},              
    { 0.5,  0, 0.5, 2, -3, 1 }, 
    { 1, -1, 2 ,0 , -3, 2 } 
  }; 

  markov_matrix(matrix);

  return 0;
}

Output

This is a Markov Matrix
/*
  C++ Program
  Determine whether matrix is Markov matrix or not
*/
#include<iostream>
#define ROW 3
#define COL 6
using namespace std;


class MyMatrix {
	public:
		int rows;
	int cols;
	MyMatrix() {
		//Get matrix size
		this->rows = ROW;
		this->cols = COL;
	}
	//Check that matrix is form of markov matrix or not
	void markov_matrix(double matrix[][COL]) {
		int status = 1;
		double sum = 0;
		for (int i = 0; i < this->rows; ++i) {
			sum = 0;
			//Calculate row sum

			for (int j = 0; j < this->cols; ++j) {
				sum += matrix[i][j];
			}
			if (sum != 1.0) {
				//When sum is not equal to 1
				status = 0;
				break;
			}
		}
		if (status == 1) {
			cout << "This is a Markov Matrix\n";
		} else {
			cout << "This is not a Markov Matrix\n";
		}
	}
};
int main() {
	double matrix[][COL] = {
		{
			-1, -1, 1, 3, -1, 0
		},
		{
			0.5, 0, 0.5, 2, -3, 1
		},
		{
			1, -1, 2, 0, -3, 2
		}
	};
	MyMatrix obj ;
	obj.markov_matrix(matrix);
	return 0;
}

Output

This is a Markov Matrix
/*
  Java Program
  Determine whether matrix is Markov matrix or not
*/
public class MyMatrix {

  public int rows;
  public int cols;
  public MyMatrix(double [][]matrix)
  {
    //Get matrix size
    this.rows = matrix.length;
    this.cols = matrix[0].length;

  }

  //Check that matrix is form of markov matrix or not
  public void markov_matrix(double [][]matrix)
  {
    
    int status=1;
    
    double sum=0;
   
    for (int i = 0; i < rows; ++i)
    {
      sum=0;
      //Calculate row sum
      for (int j = 0; j < cols; ++j)
      {
        sum+=matrix[i][j];
      }
      if(sum!=1.0)
      {
        //When sum is not equal to 1
        status=0;
        break;
      }
      
    }
    if(status==1)
    {
      System.out.print("This is a Markov Matrix\n");
    }
    else
    {
       System.out.print("This is not a Markov Matrix\n");
    }
   
  }
  public static void main(String[] args) 
  {
    //Define matrix element
    double [][]matrix =
    { 
      { -1  , -1, 1  , 3, -1, 0},              
      { 0.5,  0, 0.5, 2, -3, 1 }, 
      { 1, -1, 2 ,0 , -3, 2 } 
    }; 
    MyMatrix obj = new MyMatrix(matrix);
    obj.markov_matrix(matrix);
  }
}

Output

This is a Markov Matrix
<?php
/*
  Php Program
  Determine whether matrix is Markov matrix or not
*/
class MyMatrix {
	public $rows;
	public $cols;

	function __construct($matrix) {
		//Get matrix size
		$this->rows = count($matrix);
		$this->cols = count($matrix[0]);
	}
	//Check that matrix is form of markov matrix or not

	public 	function markov_matrix($matrix) {
		$status = 1;
		$sum = 0;
		for ($i = 0; $i < $this->rows; ++$i) {
			$sum = 0;
			//Calculate row sum

			for ($j = 0; $j < $this->cols; ++$j) {
				$sum += $matrix[$i][$j];
			}
			if ($sum != 1.0) {
				//When sum is not equal to 1
				$status = 0;
				break;
			}
		}
		if ($status == 1) {
			echo("This is a Markov Matrix\n");
		} else {
			echo("This is not a Markov Matrix\n");
		}
	}
}

function main() {
	//Define matrix element
	$matrix = array(
      array(-1, -1, 1, 3, -1, 0), 
      array(0.5, 0, 0.5, 2, -3, 1),
      array(1, -1, 2, 0, -3, 2)
    );
	$obj = new MyMatrix($matrix);
	$obj->markov_matrix($matrix);

}
main();

Output

This is a Markov Matrix
/*
  Node Js Program
  Determine whether matrix is Markov matrix or not
*/
class MyMatrix {

	constructor(matrix) {
		//Get matrix size
		this.rows = matrix.length;
		this.cols = matrix[0].length;
	}

	//Check that matrix is form of markov matrix or not
	markov_matrix(matrix) {
		var status = 1;
		var sum = 0;
		for (var i = 0; i < this.rows; ++i) {
			sum = 0;
			//Calculate row sum

			for (var j = 0; j < this.cols; ++j) {
				sum += matrix[i][j];
			}

			if (sum != 1.0) {
				//When sum is not equal to 1
				status = 0;
				break;
			}
		}

		if (status == 1) {
			process.stdout.write("This is a Markov Matrix\n");
		} else {
			process.stdout.write("This is not a Markov Matrix\n");
		}
	}
}

function main(args) {
	//Define matrix element
	var matrix = [
		[-1, -1, 1, 3, -1, 0],
		[0.5, 0, 0.5, 2, -3, 1],
		[1, -1, 2, 0, -3, 2]
	];
	var obj = new MyMatrix(matrix);
	obj.markov_matrix(matrix);
}

main();

Output

This is a Markov Matrix
# Python 3 Program
# Determine whether matrix is Markov matrix or not
class MyMatrix :
	
	def __init__(self, matrix) :
		# Get matrix size
		self.rows = len(matrix)
		self.cols = len(matrix[0])
	
	# Check that matrix is form of markov matrix or not
	def markov_matrix(self, matrix) :
		status = 1
		sum = 0
		i = 0
		while (i < self.rows) :
			sum = 0
			# Calculate row sum
			j = 0
			while (j < self.cols) :
				sum += matrix[i][j]
				j += 1
			
			if (sum != 1.0) :
				# When sum is not equal to 1
				status = 0
				break
			
			i += 1
		
		if (status == 1) :
			print("This is a Markov Matrix\n", end = "")
		else :
			print("This is not a Markov Matrix\n", end = "")
		
	

def main() :
	matrix = [
		[-1, -1, 1, 3, -1, 0],
		[0.5, 0, 0.5, 2, -3, 1],
		[1, -1, 2, 0, -3, 2]
	]
	obj = MyMatrix(matrix)
	obj.markov_matrix(matrix)


if __name__ == "__main__":
	main()

Output

This is a Markov Matrix
# Ruby Program
# Determine whether matrix is Markov matrix or not
class MyMatrix 
	# Define the accessor and reader of class MyMatrix
    attr_reader :rows, :cols
    attr_accessor :rows, :cols
	def initialize(matrix) 
		# Get matrix size
		self.rows = matrix.length
		self.cols = matrix[0].length
	end
	# Check that matrix is form of markov matrix or not
	def markov_matrix(matrix) 
		status = 1
		sum = 0
		i = 0
		while (i < @rows) 
			sum = 0
			# Calculate row sum
			j = 0
			while (j < @cols) 
				sum += matrix[i][j]
				j += 1
			end
			if (sum != 1.0) 
				# When sum is not equal to 1
				status = 0
				break
			end
			i += 1
		end
		if (status == 1) 
			print("This is a Markov Matrix\n")
		else 
			print("This is not a Markov Matrix\n")
		end
	end
end
def main() 
	matrix = [
		[-1, -1, 1, 3, -1, 0],
		[0.5, 0, 0.5, 2, -3, 1],
		[1, -1, 2, 0, -3, 2]
	]
	obj = MyMatrix.new(matrix)
	obj.markov_matrix(matrix)
end


main()

Output

This is a Markov Matrix
/*
  Scala Program
  Determine whether matrix is Markov matrix or not
*/
class MyMatrix(var rows: Int,var cols: Int) {
	def this(matrix: Array[Array[Double]]) {
		//Get matrix size
		this( matrix.length, matrix(0).length);
	}
	//Check that matrix is form of markov matrix or not
	def markov_matrix(matrix: Array[Array[Double]]): Unit = {
		var status: Int = 1;
		var sum: Double = 0;
		var i: Int = 0;
		while (i < this.rows && status == 1) {
			sum = 0;

			//Calculate row sum
			var j: Int = 0;
			while (j < this.cols) {
				sum += matrix(i)(j);
				j += 1;
			}
			if (sum != 1.0) {
				//When sum is not equal to 1
				status = 1;
			}
			i += 1;
		}
		if (status == 1) {
			print("This is a Markov Matrix\n");
		} else {
			print("This is not a Markov Matrix\n");
		}
	}
}
object Main {
	def main(args: Array[String]): Unit = {
		val matrix: Array[Array[Double]] = Array(
			Array(-1, -1, 1, 3, -1, 0),
			Array(0.5, 0, 0.5, 2, -3, 1),
			Array(1, -1, 2, 0, -3, 2));
		val obj: MyMatrix = new MyMatrix(matrix);
		obj.markov_matrix(matrix);
	}
}

Output

This is a Markov Matrix
/*
  Swift Program
  Determine whether matrix is Markov matrix or not
*/
class MyMatrix {
	var rows: Int;
	var cols: Int;
	init(_ matrix: [[Double]]) {
		//Get matrix size
		self.rows = matrix.count;
		self.cols = matrix[0].count;
	}
	//Check that matrix is form of markov matrix or not
	func markov_matrix(_ matrix: [[Double]]) {
		var status: Int = 1;
		var sum: Double = 0;
		var i: Int = 0;
		while (i < self.rows) {
			sum = 0;
			//Calculate row sum
			var j: Int = 0;
			while (j < self.cols) {
				sum += matrix[i][j];
				j += 1;
			}
			if (sum != 1.0) {
				//When sum is not equal to 1
				status = 0;
				break;
			}
			i += 1;
		}
		if (status == 1) {
			print("This is a Markov Matrix\n", terminator: "");
		} else {
			print("This is not a Markov Matrix\n", terminator: "");
		}
	}
}
func main() {
	let matrix: [
		[Double]
	] = [
		[-1, -1, 1, 3, -1, 0],
		[0.5, 0, 0.5, 2, -3, 1],
		[1, -1, 2, 0, -3, 2]
	];
	let obj: MyMatrix = MyMatrix(matrix);
	obj.markov_matrix(matrix);
}
main();

Output

This is a Markov Matrix
/*
  C# Program
  Determine whether matrix is Markov matrix or not
*/
using System;
public class MyMatrix {
	int rows;
	int cols;
	MyMatrix(double[,] matrix) {
		//Get matrix size
		this.rows = matrix.GetLength(0);
		this.cols = matrix.GetLength(1);
	}
	//Check that matrix is form of markov matrix or not
	public void markov_matrix(double[,] matrix) {
		int status = 1;
		double sum = 0;
		for (int i = 0; i < rows; ++i) {
			sum = 0;
			//Calculate row sum

			for (int j = 0; j < cols; ++j) {
				sum += matrix[i,j];
			}
			if (sum != 1.0) {
				//When sum is not equal to 1
				status = 0;
				break;;
			}
		}
		if (status == 1) {
			Console.Write("This is a Markov Matrix\n");
		} else {
			Console.Write("This is not a Markov Matrix\n");
		}
	}
	public static void Main(String[] args) {
		double[,]
		//Define matrix element
		matrix = {
			{
				-1, -1, 1, 3, -1, 0
			},
			{
				0.5,
				0,
				0.5,
				2,
				-3,
				1
			},
			{
				1,
				-1,
				2,
				0,
				-3,
				2
			}
		};
		MyMatrix obj = new MyMatrix(matrix);
		obj.markov_matrix(matrix);
	}
}

Output

This is a Markov Matrix

Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment