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

GetCol() public method

Get one column from this matrix as a column matrix.
public GetCol ( int col ) : Matrix
col int The desired column.
return Matrix
        public Matrix GetCol(int col)
        {
            if (col > Cols)
            {
                throw new MatrixError("Can't get column #" + col
                                      + " because it does not exist.");
            }

            var newMatrix = new double[Rows][];

            for (int row = 0; row < Rows; row++)
            {
                newMatrix[row] = new double[1];
                newMatrix[row][0] = matrix[row][col];
            }

            return new Matrix(newMatrix);
        }

Usage Example

Example #1
0
        public void GetCol()
        {
            double[][] matrixData1 =
            {
                new[] { 1.0, 2.0 },
                new[] { 3.0, 4.0 }
            };
            double[][] matrixData2 =
            {
                new[] { 2.0 },
                new[] { 4.0 }
            };

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

            Matrix matrixCol = matrix1.GetCol(1);

            Assert.IsTrue(matrixCol.Equals(matrix2));

            try
            {
                matrix1.GetCol(3);
                Assert.IsTrue(false);
            }
            catch (MatrixError)
            {
                Assert.IsTrue(true);
            }
        }
All Usage Examples Of Encog.MathUtil.Matrices.Matrix::GetCol