Create identity matrix in C#

static double[][] IdentityMatrix(int n)
{
	double[][] result = CreateMatrix(n, n);
	for (int i = 0; i < n; ++i)
		result[i][i] = 1.0;
	return result;
}
static double[][] CreateMatrix(int rows, int cols)
{
  double[][] result = new double[rows][];
  for (int i = 0; i < rows; ++i)
	result[i] = new double[cols]; 
  return result;
}

Enjoyed this post? Share it!

 

Leave a comment

Your email address will not be published.