sptensor函数

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
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值