# MatGetRow
Gets a row of a matrix.  You MUST call `MatRestoreRow()` for each row that you get to ensure that your application does not bleed memory. 
## Synopsis
```
#include "petscmat.h" 
PetscErrorCode MatGetRow(Mat mat, PetscInt row, PetscInt *ncols, const PetscInt *cols[], const PetscScalar *vals[])
```
Not Collective


## Input Parameters

- ***mat -*** the matrix
- ***row -*** the row to get



## Output Parameters

- ***ncols -*** if not `NULL`, the number of nonzeros in the row
- ***cols -*** if not `NULL`, the column numbers
- ***vals -*** if not `NULL`, the values





## Notes
This routine is provided for people who need to have direct access
to the structure of a matrix.  We hope that we provide enough
high-level matrix routines that few users will need it.

`MatGetRow()` always returns 0-based column indices, regardless of
whether the internal representation is 0-based (default) or 1-based.

For better efficiency, set cols and/or vals to `NULL` if you do
not wish to extract these quantities.

The user can only examine the values extracted with `MatGetRow()`;
the values cannot be altered.  To change the matrix entries, one
must use `MatSetValues()`.

You can only have one call to `MatGetRow()` outstanding for a particular
matrix at a time, per processor. `MatGetRow()` can only obtain rows
associated with the given processor, it cannot get rows from the
other processors; for that we suggest using `MatCreateSubMatrices()`, then
MatGetRow() on the submatrix. The row index passed to `MatGetRow()`
is in the global number of rows.

Use `MatGetRowIJ()` and `MatRestoreRowIJ()` to access all the local indices of the sparse matrix.

Use `MatSeqAIJGetArray()` and similar functions to access the numerical values for certain matrix types directly.


## Fortran Note
The calling sequence is
```none
   MatGetRow(matrix,row,ncols,cols,values,ierr)
         Mat     matrix (input)
         integer row    (input)
         integer ncols  (output)
         integer cols(maxcols) (output)
         double precision (or double complex) values(maxcols) output
```

where maxcols >= maximum nonzeros in any row of the matrix.


## Caution
Do not try to change the contents of the output arrays (cols and vals).
In some cases, this may corrupt the matrix.


## See Also
 `Mat`, `MatRestoreRow()`, `MatSetValues()`, `MatGetValues()`, `MatCreateSubMatrices()`, `MatGetDiagonal()`, `MatGetRowIJ()`, `MatRestoreRowIJ()`

## Level
advanced

## Location
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/interface/matrix.c.html#MatGetRow">src/mat/interface/matrix.c</A>

## Examples
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/tutorials/ex12.c.html">src/mat/tutorials/ex12.c.html</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/tutorials/ex16.c.html">src/mat/tutorials/ex16.c.html</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/ksp/ksp/tutorials/ex59.c.html">src/ksp/ksp/tutorials/ex59.c.html</A><BR>

## Implementations

<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/impls/adj/mpi/mpiadj.c.html#MatGetRow_MPIAdj">MatGetRow_MPIAdj in src/mat/impls/adj/mpi/mpiadj.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/impls/aij/mpi/mpiaij.c.html#MatGetRow_MPIAIJ">MatGetRow_MPIAIJ in src/mat/impls/aij/mpi/mpiaij.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/impls/aij/seq/aij.c.html#MatGetRow_SeqAIJ">MatGetRow_SeqAIJ in src/mat/impls/aij/seq/aij.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/impls/baij/mpi/mpibaij.c.html#MatGetRow_MPIBAIJ">MatGetRow_MPIBAIJ in src/mat/impls/baij/mpi/mpibaij.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/impls/baij/seq/baij.c.html#MatGetRow_SeqBAIJ">MatGetRow_SeqBAIJ in src/mat/impls/baij/seq/baij.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/impls/cdiagonal/cdiagonal.c.html#MatGetRow_ConstantDiagonal">MatGetRow_ConstantDiagonal in src/mat/impls/cdiagonal/cdiagonal.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/impls/dense/mpi/mpidense.c.html#MatGetRow_MPIDense">MatGetRow_MPIDense in src/mat/impls/dense/mpi/mpidense.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/impls/dense/seq/dense.c.html#MatGetRow_SeqDense">MatGetRow_SeqDense in src/mat/impls/dense/seq/dense.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/impls/htool/htool.cxx.html#MatGetRow_Htool">MatGetRow_Htool in src/mat/impls/htool/htool.cxx</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/impls/hypre/mhypre.c.html#MatGetRow_HYPRE">MatGetRow_HYPRE in src/mat/impls/hypre/mhypre.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/impls/kaij/kaij.c.html#MatGetRow_SeqKAIJ">MatGetRow_SeqKAIJ in src/mat/impls/kaij/kaij.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/impls/kaij/kaij.c.html#MatGetRow_MPIKAIJ">MatGetRow_MPIKAIJ in src/mat/impls/kaij/kaij.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/impls/sbaij/mpi/mpisbaij.c.html#MatGetRow_MPISBAIJ">MatGetRow_MPISBAIJ in src/mat/impls/sbaij/mpi/mpisbaij.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/impls/sbaij/seq/sbaij.c.html#MatGetRow_SeqSBAIJ">MatGetRow_SeqSBAIJ in src/mat/impls/sbaij/seq/sbaij.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/impls/sell/seq/sell.c.html#MatGetRow_SeqSELL">MatGetRow_SeqSELL in src/mat/impls/sell/seq/sell.c</A><BR>


---
[Edit on GitLab](https://gitlab.com/petsc/petsc/-/edit/release/src/mat/interface/matrix.c)


[Index of all Mat routines](index.md)  
[Table of Contents for all manual pages](/docs/manualpages/index.md)  
[Index of all manual pages](/docs/manualpages/singleindex.md)  
