Test if two matrices are equal in C#

static bool areMatricesEqual(double[][] matrixOne,  double[][] matrixTwo, double delta)
{
 
  int aRows = matrixOne.Length;
  int bCols = matrixTwo[0].Length;
  for (int i = 0; i < aRows; ++i) // each row of One and Two
	for (int j = 0; j < bCols; ++j) // each col of One and Two
	  if (Math.Abs(matrixOne[i][j] - matrixTwo[i][j]) > delta)
		return false;
  return true;
 }

This method does not compare cell values for exact equality because the values are type double. Instead, the method checks to see if all the cell values are very close (within delta) to each other.

Enjoyed this post? Share it!

 

Leave a comment

Your email address will not be published.