Encog.MathUtil.Matrices.Matrix.GetRow C# (CSharp) Method

GetRow() public method

Get the specified row as a row matrix.
public GetRow ( int row ) : Matrix
row int The desired row.
return Matrix
        public Matrix GetRow(int row)
        {
            if (row > Rows)
            {
                throw new MatrixError("Can't get row #" + row
                                      + " because it does not exist.");
            }

            var newMatrix = new double[1][];
            newMatrix[0] = new double[Cols];

            for (int col = 0; col < Cols; col++)
            {
                newMatrix[0][col] = matrix[row][col];
            }

            return new Matrix(newMatrix);
        }

Usage Example

        public void GetRow()
        {
            double[][] matrixData1 = {
                                         new[] {1.0, 2.0},
                                         new[] {3.0, 4.0}
                                     };
            double[][] matrixData2 = {
                                         new[] {3.0, 4.0}
                                     };

            var matrix1 = new Matrix(matrixData1);
            var matrix2 = new Matrix(matrixData2);

            Matrix matrixRow = matrix1.GetRow(1);
            Assert.IsTrue(matrixRow.Equals(matrix2));

            try
            {
                matrix1.GetRow(3);
                Assert.IsTrue(false);
            }
            catch (MatrixError)
            {
                Assert.IsTrue(true);
            }
        }