[ SciPy v1.12.0 Manual  ](../../index.html)

  * [ Installing ](https://scipy.org/install/)
  * [ User Guide ](../../tutorial/index.html)
  * [ API reference ](../index.html)
  * [ Building from source ](../../building/index.html)
  * [ Development ](../../dev/index.html)
  * [ Release notes ](../../release.html)

__ __ __

1.12.0

  * [ __ GitHub  ](https://github.com/scipy/scipy "GitHub")
  * [ __ Twitter  ](https://twitter.com/SciPy_team "Twitter")

__

  * [ scipy ](../main_namespace.html)
  * [ scipy.cluster ](../cluster.html)
  * [ scipy.constants ](../constants.html)
  * [ scipy.datasets ](../datasets.html)
  * [ scipy.fft ](../fft.html)
  * [ scipy.fftpack ](../fftpack.html)
  * [ scipy.integrate ](../integrate.html)
  * [ scipy.interpolate ](../interpolate.html)
  * [ scipy.io ](../io.html)
  * [ scipy.linalg ](../linalg.html)
  * [ scipy.misc ](../misc.html)
  * [ scipy.ndimage ](../ndimage.html)
  * [ scipy.odr ](../odr.html)
  * [ scipy.optimize ](../optimize.html)
  * [ scipy.signal ](../signal.html)
  * [ scipy.sparse ](../sparse.html)
  * [ scipy.spatial ](../spatial.html)
  * [ scipy.special ](../special.html)
  * [ scipy.stats ](../stats.html)

__ On this page

  * ` mmwrite  `

#  scipy.io.mmwrite  #

scipy.io.  mmwrite  (  _ target  _ , _ a  _ , _ comment  =  None  _ , _ field
=  None  _ , _ precision  =  None  _ , _ symmetry  =  'AUTO'  _ )  [ [source]
](https://github.com/scipy/scipy/blob/v1.12.0/scipy/io/_fast_matrix_market/__init__.py#L369-L533)
#

    

Writes the sparse or dense array _a_ to Matrix Market file-like _target_ .

Parameters  :

    

**target** str or file-like

    

Matrix Market filename (extension .mtx) or open file-like object.

**a** array like

    

Sparse or dense 2-D array.

**comment** str, optional

    

Comments to be prepended to the Matrix Market file.

**field** None or str, optional

    

Either ârealâ, âcomplexâ, âpatternâ, or âintegerâ.

**precision** None or int, optional

    

Number of digits to display for real or complex values.

**symmetry** None or str, optional

    

Either âAUTOâ, âgeneralâ, âsymmetricâ, âskew-symmetricâ, or
âhermitianâ. If symmetry is None the symmetry type of âaâ is
determined by its values. If symmetry is âAUTOâ the symmetry type of
âaâ is either determined or set to âgeneralâ, at mmwriteâs
discretion.

Returns  :

    

None

    

Notes

Changed in version 1.12.0:  C++ implementation.

Examples

    
    
    >>> from io import BytesIO
    >>> import numpy as np
    >>> from scipy.sparse import coo_matrix
    >>> from scipy.io import mmwrite
    

Write a small NumPy array to a matrix market file. The file will be written in
the ` 'array'  ` format.

    
    
    >>> a = np.array([[1.0, 0, 0, 0], [0, 2.5, 0, 6.25]])
    >>> target = BytesIO()
    >>> mmwrite(target, a)
    >>> print(target.getvalue().decode('latin1'))
    %%MatrixMarket matrix array real general
    %
    2 4
    1
    0
    0
    2.5
    0
    0
    0
    6.25
    

Add a comment to the output file, and set the precision to 3.

    
    
    >>> target = BytesIO()
    >>> mmwrite(target, a, comment='\n Some test data.\n', precision=3)
    >>> print(target.getvalue().decode('latin1'))
    %%MatrixMarket matrix array real general
    %
    % Some test data.
    %
    2 4
    1.00e+00
    0.00e+00
    0.00e+00
    2.50e+00
    0.00e+00
    0.00e+00
    0.00e+00
    6.25e+00
    

Convert to a sparse matrix before calling ` mmwrite  ` . This will result in
the output format being ` 'coordinate'  ` rather than ` 'array'  ` .

    
    
    >>> target = BytesIO()
    >>> mmwrite(target, coo_matrix(a), precision=3)
    >>> print(target.getvalue().decode('latin1'))
    %%MatrixMarket matrix coordinate real general
    %
    2 4 3
    1 1 1.00e+00
    2 2 2.50e+00
    2 4 6.25e+00
    

Write a complex Hermitian array to a matrix market file. Note that only six
values are actually written to the file; the other values are