Converting Sparse Tensors to Matrices and Vice Versa

We show how to convert a sptensor to a matrix stored in coordinate format and with extra information so that it can be converted back to a sptensor.

Creating a sptenmat (sparse tensor as sparse matrix) object

A sparse tensor can be converted to a sparse matrix. The matrix, however, is not stored as a MATLAB sparse matrix because that format is sometimes inefficient for converted sparse tensors. Instead, the row and column indices are stored explicitly.

First, we create a sparse tensor to be converted.

```X = sptenrand([10 10 10 10],10) %<-- Generate some data.
```
```X is a sparse tensor of size 10 x 10 x 10 x 10 with 10 nonzeros
(1,5,5, 3)    0.6408
(1,5,8, 1)    0.1909
(1,6,2, 3)    0.8439
(1,7,8, 3)    0.1739
(1,8,5, 9)    0.1708
(2,4,9, 1)    0.9943
(4,1,7, 3)    0.4398
(7,1,2, 3)    0.3400
(7,4,7, 9)    0.3142
(7,7,6,10)    0.3651
```

All the same options for tenmat are available as for tenmat.

```A = sptenmat(X,1) %<-- Mode-1 matricization.
```
```A is a sptenmat from an sptensor of size 10 x 10 x 10 x 10 with 10 nonzeros
A.rindices = [ 1 ] (modes of tensor corresponding to rows)
A.cindices = [ 2  3  4 ] (modes of tensor corresponding to columns)
(1,245)	0.640815
(1, 75)	0.190887
(1,216)	0.843869
(1,277)	0.1739
(1,848)	0.170793
(2, 84)	0.994295
(4,261)	0.439791
(7,211)	0.340048
(7,864)	0.314217
(7,957)	0.365078
```
```A = sptenmat(X,[2 3]) %<-- More than one mode is mapped to the columns.
```
```A is a sptenmat from an sptensor of size 10 x 10 x 10 x 10 with 10 nonzeros
A.rindices = [ 2  3 ] (modes of tensor corresponding to rows)
A.cindices = [ 1  4 ] (modes of tensor corresponding to columns)
(45,21)	0.640815
(75, 1)	0.190887
(16,21)	0.843869
(77,21)	0.1739
(48,81)	0.170793
(84, 2)	0.994295
(61,24)	0.439791
(11,27)	0.340048
(64,87)	0.314217
(57,97)	0.365078
```
```A = sptenmat(X,[2 3],'t') %<-- Specify column dimensions (transpose).
```
```A is a sptenmat from an sptensor of size 10 x 10 x 10 x 10 with 10 nonzeros
A.rindices = [ 1  4 ] (modes of tensor corresponding to rows)
A.cindices = [ 2  3 ] (modes of tensor corresponding to columns)
(21,45)	0.640815
( 1,75)	0.190887
(21,16)	0.843869
(21,77)	0.1739
(81,48)	0.170793
( 2,84)	0.994295
(24,61)	0.439791
(27,11)	0.340048
(87,64)	0.314217
(97,57)	0.365078
```
```A = sptenmat(X,1:4) %<-- All modes mapped to rows, i.e., vectorize.
```
```A is a sptenmat from an sptensor of size 10 x 10 x 10 x 10 with 10 nonzeros
A.rindices = [ 1  2  3  4 ] (modes of tensor corresponding to rows)
A.cindices = [  ] (modes of tensor corresponding to columns)
(2441,1)	0.640815
( 741,1)	0.190887
(2151,1)	0.843869
(2761,1)	0.1739
(8471,1)	0.170793
( 832,1)	0.994295
(2604,1)	0.439791
(2107,1)	0.340048
(8637,1)	0.314217
(9567,1)	0.365078
```
```A = sptenmat(X,2) %<-- By default, columns are ordered as [1 3 4].
```
```A is a sptenmat from an sptensor of size 10 x 10 x 10 x 10 with 10 nonzeros
A.rindices = [ 2 ] (modes of tensor corresponding to rows)
A.cindices = [ 1  3  4 ] (modes of tensor corresponding to columns)
(5,241)	0.640815
(5, 71)	0.190887
(6,211)	0.843869
(7,271)	0.1739
(8,841)	0.170793
(4, 82)	0.994295
(1,264)	0.439791
(1,217)	0.340048
(4,867)	0.314217
(7,957)	0.365078
```
```A = sptenmat(X,2,[3 1 4]) %<-- Explicit column ordering.
```
```A is a sptenmat from an sptensor of size 10 x 10 x 10 x 10 with 10 nonzeros
A.rindices = [ 2 ] (modes of tensor corresponding to rows)
A.cindices = [ 3  1  4 ] (modes of tensor corresponding to columns)
(5,205)	0.640815
(5,  8)	0.190887
(6,202)	0.843869
(7,208)	0.1739
(8,805)	0.170793
(4, 19)	0.994295
(1,237)	0.439791
(1,262)	0.340048
(4,867)	0.314217
(7,966)	0.365078
```
```A = sptenmat(X,2,'fc') %<-- Foward cyclic.
```
```A is a sptenmat from an sptensor of size 10 x 10 x 10 x 10 with 10 nonzeros
A.rindices = [ 2 ] (modes of tensor corresponding to rows)
A.cindices = [ 3  4  1 ] (modes of tensor corresponding to columns)
(5, 25)	0.640815
(5,  8)	0.190887
(6, 22)	0.843869
(7, 28)	0.1739
(8, 85)	0.170793
(4,109)	0.994295
(1,327)	0.439791
(1,622)	0.340048
(4,687)	0.314217
(7,696)	0.365078
```
```A = sptenmat(X,2,'bc') %<-- Backward cyclic.
```
```A is a sptenmat from an sptensor of size 10 x 10 x 10 x 10 with 10 nonzeros
A.rindices = [ 2 ] (modes of tensor corresponding to rows)
A.cindices = [ 1  4  3 ] (modes of tensor corresponding to columns)
(5,421)	0.640815
(5,701)	0.190887
(6,121)	0.843869
(7,721)	0.1739
(8,481)	0.170793
(4,802)	0.994295
(1,624)	0.439791
(1,127)	0.340048
(4,687)	0.314217
(7,597)	0.365078
```

Constituent parts of a sptenmat

```A.subs %<-- Subscripts of the nonzeros.
```
```ans =
5   421
5   701
6   121
7   721
8   481
4   802
1   624
1   127
4   687
7   597
```
```A.vals %<-- The corresponding nonzero values.
```
```ans =
0.6408
0.1909
0.8439
0.1739
0.1708
0.9943
0.4398
0.3400
0.3142
0.3651
```
```A.tsize %<-- Size of the original tensor.
```
```ans =
10    10    10    10
```
```A.rdims %<-- Dimensions that were mapped to the rows.
```
```ans =
2
```
```A.cdims %<-- Dimensions that were mapped to the columns.
```
```ans =
1     4     3
```

Creating a sptenmat from its constituent parts

```B = sptenmat(A.subs,A.vals,A.rdims,A.cdims,A.tsize) %<-- Copies A
```
```B is a sptenmat from an sptensor of size 10 x 10 x 10 x 10 with 10 nonzeros
B.rindices = [ 2 ] (modes of tensor corresponding to rows)
B.cindices = [ 1  4  3 ] (modes of tensor corresponding to columns)
(1,127)	0.340048
(1,624)	0.439791
(4,687)	0.314217
(4,802)	0.994295
(5,421)	0.640815
(5,701)	0.190887
(6,121)	0.843869
(7,597)	0.365078
(7,721)	0.1739
(8,481)	0.170793
```
```B = sptenmat(double(A),A.rdims,A.cdims,A.tsize) %<-- More efficient to pass a matrix.
```
```B is a sptenmat from an sptensor of size 10 x 10 x 10 x 10 with 10 nonzeros
B.rindices = [ 2 ] (modes of tensor corresponding to rows)
B.cindices = [ 1  4  3 ] (modes of tensor corresponding to columns)
(6,121)	0.843869
(1,127)	0.340048
(5,421)	0.640815
(8,481)	0.170793
(7,597)	0.365078
(1,624)	0.439791
(4,687)	0.314217
(5,701)	0.190887
(7,721)	0.1739
(4,802)	0.994295
```

Creating a sptenmat with no nonzeros

```A = sptenmat([],[],A.rdims,A.cdims,A.tsize) %<-- An empty sptenmat.
```
```A is an all-zero sptenmat from an sptensor of size 10 x 10 x 10 x 10
A.rindices = [ 2 ] (modes of tensor corresponding to rows)
A.cindices = [ 1  4  3 ] (modes of tensor corresponding to columns)
```

Creating an emtpy sptenmat

```A = sptenmat %<-- A really empty sptenmat.
```
```A is an all-zero sptenmat from an sptensor of size [empty tensor]
A.rindices = [  ] (modes of tensor corresponding to rows)
A.cindices = [  ] (modes of tensor corresponding to columns)
```

Use double to convert a sptenmat to a MATLAB sparse matrix

```X = sptenrand([10 10 10 10],10); %<-- Create a tensor.
A = sptenmat(X,1) %<-- Convert it to a sptenmat
```
```A is a sptenmat from an sptensor of size 10 x 10 x 10 x 10 with 10 nonzeros
A.rindices = [ 1 ] (modes of tensor corresponding to rows)
A.cindices = [ 2  3  4 ] (modes of tensor corresponding to columns)
( 1,417)	0.982988
( 2,645)	0.552673
( 2,440)	0.400074
( 3, 89)	0.198789
( 4,473)	0.625201
( 5,919)	0.733363
( 6,467)	0.375885
( 9, 11)	0.00987646
( 9,689)	0.419858
(10,242)	0.75367
```
```B = double(A) %<-- Convert it to a MATLAB sparse matrix
```
```B =
(9,11)      0.0099
(3,89)      0.1988
(10,242)     0.7537
(1,417)     0.9830
(2,440)     0.4001
(6,467)     0.3759
(4,473)     0.6252
(2,645)     0.5527
(9,689)     0.4199
(5,919)     0.7334
```
```whos A B %<-- The storage for B (the sparse matrix) is larger than for A.
```
```  Name       Size              Bytes  Class       Attributes

A         10x1000             1184  sptenmat
B         10x1000             8168  double      sparse

```
```C = B'; %<-- Transposing the result fixes the problem.
whos C
```
```  Name         Size            Bytes  Class     Attributes

C         1000x10              248  double    sparse

```

Use full to convert a sptenmat to a tenmat

```B = sptenmat(sptenrand([3 3 3], 3), 1) %<-- Create a sptenmat
```
```B is a sptenmat from an sptensor of size 3 x 3 x 3 with 3 nonzeros
B.rindices = [ 1 ] (modes of tensor corresponding to rows)
B.cindices = [ 2  3 ] (modes of tensor corresponding to columns)
(3,2)	0.63179
(3,8)	0.234413
(3,6)	0.548782
```
```C = full(B) %<-- Convert to a tenmat
```
```C is a matrix corresponding to a tensor of size 3 x 3 x 3
C.rindices = [ 1 ] (modes of tensor corresponding to rows)
C.cindices = [ 2  3 ] (modes of tensor corresponding to columns)
C.data =
Columns 1 through 7
0         0         0         0         0         0         0
0         0         0         0         0         0         0
0    0.6318         0         0         0    0.5488         0
Columns 8 through 9
0         0
0         0
0.2344         0
```

Use sptensor to convert a sptenmat to a sptensor

```Y = sptensor(A) %<-- Convert a sptenmat to a sptensor
```
```Y is a sparse tensor of size 10 x 10 x 10 x 10 with 10 nonzeros
( 1, 7,2, 5)    0.9830
( 2, 5,5, 7)    0.5527
( 2,10,4, 5)    0.4001
( 3, 9,9, 1)    0.1988
( 4, 3,8, 5)    0.6252
( 5, 9,2,10)    0.7334
( 6, 7,7, 5)    0.3759
( 9, 1,2, 1)    0.0099
( 9, 9,9, 7)    0.4199
(10, 2,5, 3)    0.7537
```

Use size and tsize for the dimensions of a sptenmat

```size(A) %<-- Matrix size
tsize(A) %<-- Corresponding tensor size
```
```ans =
10        1000
ans =
10    10    10    10
```

Subscripted reference for a sptenmat

This is not supported beyond getting the constituent parts.

Subscripted assignment for a sptenmat

```A(1:2,1:2) = ones(2) %<-- Replace part of the matrix.
```
```A is a sptenmat from an sptensor of size 10 x 10 x 10 x 10 with 14 nonzeros
A.rindices = [ 1 ] (modes of tensor corresponding to rows)
A.cindices = [ 2  3  4 ] (modes of tensor corresponding to columns)
( 1,  1)	1
( 1,  2)	1
( 1,417)	0.982988
( 2,  1)	1
( 2,  2)	1
( 2,440)	0.400074
( 2,645)	0.552673
( 3, 89)	0.198789
( 4,473)	0.625201
( 5,919)	0.733363
( 6,467)	0.375885
( 9, 11)	0.00987646
( 9,689)	0.419858
(10,242)	0.75367
```

Use end for the last index

End is not supported.

Basic operations for sptenmat

```norm(A) %<-- Norm of the matrix.
```
```ans =
2.6992
```
```+A %<-- Calls uplus.
```
```ans is a sptenmat from an sptensor of size 10 x 10 x 10 x 10 with 14 nonzeros
ans.rindices = [ 1 ] (modes of tensor corresponding to rows)
ans.cindices = [ 2  3  4 ] (modes of tensor corresponding to columns)
( 1,  1)	1
( 1,  2)	1
( 1,417)	0.982988
( 2,  1)	1
( 2,  2)	1
( 2,440)	0.400074
( 2,645)	0.552673
( 3, 89)	0.198789
( 4,473)	0.625201
( 5,919)	0.733363
( 6,467)	0.375885
( 9, 11)	0.00987646
( 9,689)	0.419858
(10,242)	0.75367
```
```-A %<-- Calls uminus.
```
```ans is a sptenmat from an sptensor of size 10 x 10 x 10 x 10 with 14 nonzeros
ans.rindices = [ 1 ] (modes of tensor corresponding to rows)
ans.cindices = [ 2  3  4 ] (modes of tensor corresponding to columns)
( 1,  1)	-1
( 1,  2)	-1
( 1,417)	-0.982988
( 2,  1)	-1
( 2,  2)	-1
( 2,440)	-0.400074
( 2,645)	-0.552673
( 3, 89)	-0.198789
( 4,473)	-0.625201
( 5,919)	-0.733363
( 6,467)	-0.375885
( 9, 11)	-0.00987646
( 9,689)	-0.419858
(10,242)	-0.75367
```

Use aatx to efficiently compute A * A' * x for a sptenmat

```x = ones(10,1); %<-- Create vector
aatx(A,x) %<-- Compute A * A' * x
```
```ans =
4.9663
4.4655
0.0395
0.3909
0.5378
0.1413
0
0
0.1764
0.5680
```
```double(A) * double(A)' * x %<-- Same as above but less efficient
```
```ans =
4.9663
4.4655
0.0395
0.3909
0.5378
0.1413
0
0
0.1764
0.5680
```

Displaying a tenmat

Shows the original tensor dimensions, the modes mapped to rows, the modes mapped to columns, and the matrix.

```disp(A)
```
```ans is a sptenmat from an sptensor of size 10 x 10 x 10 x 10 with 14 nonzeros
ans.rindices = [ 1 ] (modes of tensor corresponding to rows)
ans.cindices = [ 2  3  4 ] (modes of tensor corresponding to columns)
( 1,  1)	1
( 1,  2)	1
( 1,417)	0.982988
( 2,  1)	1
( 2,  2)	1
( 2,440)	0.400074
( 2,645)	0.552673
( 3, 89)	0.198789
( 4,473)	0.625201
( 5,919)	0.733363
( 6,467)	0.375885
( 9, 11)	0.00987646
( 9,689)	0.419858
(10,242)	0.75367
```