Which is faster - SetSubMatrix vs interative copy of a column

Hello, just getting started with MatNET.Numerics. I am porting some Matlab code to C#.

I frequently need to copy a column of a Matrix to another Matrix (same number of rows). Making the code run fast is paramount. Is it faster to create a for loop, and copy each individual element, or should I be using SetSubMatrix()? E.g.:

for (int i = 0; i < matrixA.RowCount; ++i)
    matrixA.At(i, myNewCol, matrixB.At(i, myOldCol));


matrixA.SetSubMatrix(0, myNewCol, matrixB.SubMatrix(0, matrixB.RowCount, myOldCol, 1));

Which is faster?

The rule of thumb is that when you can choose between A) a builtin function like SetSubMatrix and B) calling indexers or At-methods in a loop, then A is typically much faster as often it is operating directly on the internal arrays, meaning less overhead with calls, index computations and range checking.

1 Like