Accelerating the pace of engineering and science

• Trials

# spconvert

Import matrix from sparse matrix external format

S = spconvert(D)

## Description

spconvert is used to create sparse matrices from a simple sparse format easily produced by non-MATLAB sparse programs. spconvert is the second step in the process:

1. Load an ASCII data file containing [i,j,v] or [i,j,re,im] as rows into a MATLAB® variable.

2. Convert that variable into a MATLAB sparse matrix.

S = spconvert(D) converts a matrix D with rows containing [i,j,s] or [i,j,r,s] to the corresponding sparse matrix. D must have an nnz or nnz+1 row and three or four columns. Three elements per row generate a real matrix and four elements per row generate a complex matrix. A row of the form [m n 0] or [m n 0 0] anywhere in D can be used to specify size(S). If D is already sparse, no conversion is done, so spconvert can be used after D is loaded from either a MAT-file or an ASCII file.

## Examples

Suppose the ASCII file uphill.dat contains

```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```

Then the statements

```load uphill.dat
H = spconvert(uphill)

H =
(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```

recreate sparse(triu(hilb(4))), possibly with roundoff errors. In this case, the last line of the input file is not necessary because the earlier lines already specify that the matrix is at least 4-by-4.