spensor 用来创建一个稀疏张量。
使用帮助文档help sptensor 可以显示如下.
sptensor Create a sparse tensor.
X = sptensor(SUBS, VALS, SZ, FUN) uses the rows of SUBS and VALS
to generate a sparse tensor X of size SZ = [m1 m2 ... mn]. SUBS is
an p x n array specifying the subscripts of the values to be
inserted into S. The k-th row of SUBS specifies the subscripts for
the k-th value in VALS. The values are accumulated at repeated
subscripts using the function FUN, which is specified by a
function handle.
解释
subs坐标,vals值,sz张量的size,
其中subs第k个坐标填入vals中的第k个值,如果subs中有出重复地坐标,则参数fun说明了如何操作。
默认是将几个相同的subs所对应的vals的值相加,填入该坐标。
There are several simplifications of this four argument call.
X = sptensor(SUBS,VALS,SZ) uses FUN=@SUM.
X = sptensor(SUBS,VALS) uses SM = max(SUBS,[],1).
X = sptensor(SZ) abbreviates X = sptensor([],[],SZ).
X = sptensor(Y) copies/converts Y if it is an sptensor, an sptenmat, or
a dense tensor or MDA (the zeros are squeezed out), an sptensor3, or a
sparse matrix. Note that a row-vector, integer MDA is interpreted as a
size (see previous constructor).
S = sptensor is the empty constructor.
The argument VALS may be scalar, which is expanded to be the
same length as SUBS, i.e., it is equivalent to VALS*(p,1).
Example:
>> subs = [1 1 1; 1 1 3; 2 2 2; 4 4 4; 1 1 1; 1 1 1]
vals = [0.5; 1.5; 2.5; 3.5; 4.5; 5.5]
siz = [4 4 4];
X = sptensor(subs,vals,siz) %<-- sparse 4x4x4, repeats summed 重复的值进行求和。默认
X = sptensor(subs,1,siz) %<-- scalar 2nd argument
X = sptensor(subs,vals,siz, @max) %<-- max for accumulation 重复的值使用最大值
myfun = @(x) sum(x) / 3;
X = sptensor(subs,vals,siz,myfun) %<-- custom accumulation
subs =
1 1 1
1 1 3
2 2 2
4 4 4
1 1 1
1 1 1
vals =
0.5000
1.5000
2.5000
3.5000
4.5000
5.5000
X is a sparse tensor of size 4 x 4 x 4 with 4 nonzeros
(1,1,1) 10.5000
(1,1,3) 1.5000
(2,2,2) 2.5000
(4,4,4) 3.5000
X is a sparse tensor of size 4 x 4 x 4 with 4 nonzeros
(1,1,1) 3
(1,1,3) 1
(2,2,2) 1
(4,4,4) 1
X is a sparse tensor of size 4 x 4 x 4 with 4 nonzeros
(1,1,1) 5.5000
(1,1,3) 1.5000
(2,2,2) 2.5000
(4,4,4) 3.5000
X is a sparse tensor of size 4 x 4 x 4 with 4 nonzeros
(1,1,1) 3.5000
(1,1,3) 0.5000
(2,2,2) 0.8333
(4,4,4) 1.1667