def METHOD_NAME(builder, n_rows, n_cols, columns, col_idx):
    """Serialize NetEncoding.Matrix"""
    Matrix.MatrixStart(builder)
    Matrix.MatrixAddNRows(builder, n_rows)
    Matrix.MatrixAddNCols(builder, n_cols)
    Matrix.MatrixAddColumns(builder, columns)
    if col_idx is not None:
        (union_type, u_val) = col_idx
        Matrix.MatrixAddColIndexType(builder, union_type)
        Matrix.MatrixAddColIndex(builder, u_val)
    return Matrix.MatrixEnd(builder)