1

I have an image that is stored in a DenseMatrix, using MathNet Numerics.

For rotating the image by 90 degrees counter-clockwise I want to get the transpose and then flip the result vertically by multiplying by the anti-diagonal identity matrix.

Is there a quick way to initialize that identity matrix?

For a 2x2 matrix that would look like:

0 1 
1 0

Update:

I ended up doing pretty much what @Joseph suggested. Turns out to be sufficiently fast.

public static Matrix<double> CreateAntiIdentityMatrix(int n)
{
    var output = Matrix<double>.Build.Dense(n, n, 0);
    for (int i = 0; i <= n - 1; i++)
    {
        output[i, n - i - 1] = 1;
    }
    return output;
}
TylerH
  • 20,799
  • 66
  • 75
  • 101
tde
  • 150
  • 13

2 Answers2

2

Something like this should work:

var M = MathNet.Numerics.LinearAlgebra.Double.Matrix.Build.Dense(N, N, 0);
for (i = 0; i <= N - 1; i++)
{
    M(i, N - i - 1) = 1;
}
xdtTransform
  • 1,986
  • 14
  • 34
Joseph
  • 380
  • 3
  • 16
0

@Joseph's way is fast. But I'd like to introduce a way which is expressively shows MathNet functionality:

var size = 3;
var diagonal = DenseMatrix.CreateDiagonal(size, size, 1);
Console.WriteLine(diagonal);

var reversedColumns = diagonal.EnumerateColumns().Select(c => c.Reverse());
var anti = DenseMatrix.OfColumns(reversedColumns);
Console.WriteLine(anti);

To get an anti-diagonal matrix one can take a diagonal one and reflect it by width (reverse columns).

Result is:

DenseMatrix 3x3-Double
1  0  0
0  1  0
0  0  1

DenseMatrix 3x3-Double
0  0  1
0  1  0
1  0  0
CSDev
  • 3,177
  • 6
  • 19
  • 37