Quantcast
Channel: Math.NET Numerics
Viewing all articles
Browse latest Browse all 971

New Post: SubMatrix copy

$
0
0
I have a matrix and I want to swap quandrant 1 with quandrant 3 and quandrant 2 with quadrant 4. For example

1 2 3
4 5 6
7 8 9

becomes

9 7 8
3 1 2
6 4 5

I can do this with the MathNet SubMatrix method as shown below. I will typically be doing this on matrices the size of 1000 x 1000. Is this the most efficient way? Is there a way to do this with BlockCopy?
public static double[,] MatrixShift(double [,] a)
{
    var matA = DenseMatrix.OfArray(a);

    var N = a.GetLength(0);
    var M = a.GetLength(1);
    var n = (int)Math.Ceiling(N / 2.0);
    var m = (int)Math.Ceiling(M / 2.0);

    var A11 = matA.SubMatrix(0, n, 0, m);
    var A12 = matA.SubMatrix(0, n, m, M - m);
    var A21 = matA.SubMatrix(n, N - n, 0, m);
    var A22 = matA.SubMatrix(n, N-n, m, M-m);

    var B = new DenseMatrix(N,M);

    B.SetSubMatrix(0, 0, A22);
    B.SetSubMatrix(0, M - m, A21);
    B.SetSubMatrix(N-n, 0, A12);
    B.SetSubMatrix(N-n, M-m, A11);

    return B.ToArray();
}

Viewing all articles
Browse latest Browse all 971


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>