'Matrix concatenation using MathNet.Numerics.LinearAlgebra in .NET Framework

I would like to perform row-wise and column-wise matrix concatenation using .NET Framework with MathNet library. For example, if I have 3 matrices A, B, C of dimension nxn, I would like create a new matrix D of dimension 2nx2n where

// I have used MATLAB syntax here to explain the operation
// D = [A B; B C];

I have currently defined the matrices A, B, C as DenseMatrix class and would like to proceed from here.



Solution 1:[1]

That's one way to do it in VB.Net:

Dim D =
    MathNet.Numerics.LinearAlgebra.Double.Matrix.Build.DenseOfMatrixArray(
        New MathNet.Numerics.LinearAlgebra.Matrix(Of Double)(,)
             {{A, B}, {B, C}})

Solution 2:[2]

In C# you could do:

// To shorten build notation.
var M = Matrix<double>.Build;

// Define these matrices how you want it.
Matrix<double> A = ...;
Matrix<double> B = ...;
Matrix<double> C = ...;

// Define a 2D array of matrices.
Matrix<double>[,] MatrixArray2D =
{  
    { A, B },
    { B, C }
};

// Use the 2D array to construct the concatenated matrix.
Matrix<double> ConcatinatedMatrix = M.DenseOfMatrixArray(MatrixArray2D);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Stefan
Solution 2