# spconvert

Import from sparse matrix external format

## Syntax

``S = spconvert(D)``

## Description

````S = spconvert(D)` constructs sparse matrix `S` from the columns of `D` in a manner similar to the `sparse` function.If `D` is of size `N`-by-`3`, then `spconvert` uses the columns `[i,j,re]` of `D` to construct `S`, such that ```S(i(k), j(k)) = re(k)```.If `D` is of size `N`-by-`4`, then `spconvert` uses the columns `[i,j,re,im]` of `D` to construct `S`, such that ```S(i(k), j(k)) = re(k) + 1i*im(k)```.```

example

## Examples

collapse all

Create an ASCII file, `uphill.dat`, which contains the following values. Save the file in your current directory.

```1 1 1.000000000000000 1 2 0.500000000000000 2 2 0.333333333333333 1 3 0.333333333333333 2 3 0.250000000000000 3 3 0.200000000000000 1 4 0.250000000000000 2 4 0.200000000000000 3 4 0.166666666666667 4 4 0.142857142857143 4 4 0.000000000000000```

It is common to purposefully make the last line of the file include the desired size of the matrix with a value of 0. This practice ensures that the converted sparse matrix has that size.

Load the data into MATLAB® and convert it into a sparse matrix.

```load uphill.dat H = spconvert(uphill) ```
```H = 4×4 sparse double matrix (10 nonzeros) (1,1) 1.0000 (1,2) 0.5000 (2,2) 0.3333 (1,3) 0.3333 (2,3) 0.2500 (3,3) 0.2000 (1,4) 0.2500 (2,4) 0.2000 (3,4) 0.1667 (4,4) 0.1429```

In this case, the last line in the file is not necessary because the earlier lines already specify that the matrix is at least 4-by-4.

## Input Arguments

collapse all

Input matrix, specified as a matrix with either three or four columns. In both cases, the first two columns of `D` are subscripts and the third column is composed of data values. A four column matrix specifies the real (third column) and imaginary (fourth column) parts of complex numbers.

If `D` is already a sparse matrix, then `spconvert` returns `D`.

Data Types: `single` | `double`

## Version History

Introduced before R2006a