Collapsing and scaling tensors

The tensor and sptensor classes support that notion of collapsing and scaling dimensions.

Contents

Examples of collapsing a tensor

X = tenrand([4 3 2]) %<-- Generate some data.
X is a tensor of size 4 x 3 x 2
	X(:,:,1) = 
	    0.6408    0.1708    0.3142
	    0.1909    0.9943    0.3651
	    0.8439    0.4398    0.3932
	    0.1739    0.3400    0.5915
	X(:,:,2) = 
	    0.1197    0.9342    0.2379
	    0.0381    0.2644    0.6458
	    0.4586    0.1603    0.9669
	    0.8699    0.8729    0.6649
Y = collapse(X,[2 3]) %<-- Sum of entries in each mode-1 slice.
Y is a tensor of size 4
	Y(:) = 
	    2.4177
	    2.4987
	    3.2627
	    3.5131
Y = collapse(X,-1) %<-- Same as above.
Y is a tensor of size 4
	Y(:) = 
	    2.4177
	    2.4987
	    3.2627
	    3.5131
Z = collapse(X,2) %<-- Sum of entries in each row fiber.
Z is a tensor of size 4 x 2
	Z(:,:) = 
	    1.1258    1.2919
	    1.5503    0.9484
	    1.6769    1.5858
	    1.1055    2.4077
collapse(X,1:3) %<-- Sum of all entries.
ans =
   11.6922

Alternate accumulation functions for tensor

Y = collapse(X,[1 2],@max) %<-- Max entry in each mode-3 slice.
Y is a tensor of size 2
	Y(:) = 
	    0.9943
	    0.9669
Z = collapse(X,-3,@mean) %<-- Average entry in each mode-3 slice.
Z is a tensor of size 2
	Z(:) = 
	    0.4549
	    0.5195

Examples of collapsing a sptensor

X = sptenrand([4 3 2],6) %<-- Generate some data.
X is a sparse tensor of size 4 x 3 x 2 with 6 nonzeros
	(1,2,1)    0.4507
	(1,3,2)    0.4122
	(2,1,1)    0.9016
	(4,1,1)    0.0056
	(4,1,2)    0.2974
	(4,3,1)    0.0492
Y = collapse(X,[2 3]) %<-- Sum of entries in each mode-1 slice.
Y =
    0.8629
    0.9016
         0
    0.3522
Y = collapse(X,-1) %<-- Same as above.
Y =
    0.8629
    0.9016
         0
    0.3522
Z = collapse(X,2) %<-- Sum of entries in each row fiber.
Z is a sparse tensor of size 4 x 2 with 5 nonzeros
	(1,1)    0.4507
	(1,2)    0.4122
	(2,1)    0.9016
	(4,1)    0.0547
	(4,2)    0.2974
collapse(X,1:3) %<-- Sum of all entries.
ans =
    2.1167

Alternate accumulation functions for sptensor

Y = collapse(X,[1 2],@min) %<-- Min *nonzero* entry in each mode-3 slice.
Y =
    0.0056
    0.2974
Z = collapse(X,-3,@mean) %<-- Average *nonzero* entry in each mode-3 slice.
Z =
    0.3518
    0.3548

Scaling a tensor in different modes

X = tenones([3,4,5]); %<-- Generate data
S = 10 * [1:5]'; Y = scale(X,S,3) %<-- Scale in mode-3
Y is a tensor of size 3 x 4 x 5
	Y(:,:,1) = 
	    10    10    10    10
	    10    10    10    10
	    10    10    10    10
	Y(:,:,2) = 
	    20    20    20    20
	    20    20    20    20
	    20    20    20    20
	Y(:,:,3) = 
	    30    30    30    30
	    30    30    30    30
	    30    30    30    30
	Y(:,:,4) = 
	    40    40    40    40
	    40    40    40    40
	    40    40    40    40
	Y(:,:,5) = 
	    50    50    50    50
	    50    50    50    50
	    50    50    50    50
S = tensor(10 * [1:5]',5); Y = scale(X,S,3) %<-- First argument is a tensor.
Y is a tensor of size 3 x 4 x 5
	Y(:,:,1) = 
	    10    10    10    10
	    10    10    10    10
	    10    10    10    10
	Y(:,:,2) = 
	    20    20    20    20
	    20    20    20    20
	    20    20    20    20
	Y(:,:,3) = 
	    30    30    30    30
	    30    30    30    30
	    30    30    30    30
	Y(:,:,4) = 
	    40    40    40    40
	    40    40    40    40
	    40    40    40    40
	Y(:,:,5) = 
	    50    50    50    50
	    50    50    50    50
	    50    50    50    50
S = tensor(1:12,[3 4]); Y = scale(X,S,[1 2]) %<-- Scale in two modes.
Y is a tensor of size 3 x 4 x 5
	Y(:,:,1) = 
	     1     4     7    10
	     2     5     8    11
	     3     6     9    12
	Y(:,:,2) = 
	     1     4     7    10
	     2     5     8    11
	     3     6     9    12
	Y(:,:,3) = 
	     1     4     7    10
	     2     5     8    11
	     3     6     9    12
	Y(:,:,4) = 
	     1     4     7    10
	     2     5     8    11
	     3     6     9    12
	Y(:,:,5) = 
	     1     4     7    10
	     2     5     8    11
	     3     6     9    12
S = tensor(1:12,[3 4]); Y = scale(X,S,-3) %<-- Same as above.
Y is a tensor of size 3 x 4 x 5
	Y(:,:,1) = 
	     1     4     7    10
	     2     5     8    11
	     3     6     9    12
	Y(:,:,2) = 
	     1     4     7    10
	     2     5     8    11
	     3     6     9    12
	Y(:,:,3) = 
	     1     4     7    10
	     2     5     8    11
	     3     6     9    12
	Y(:,:,4) = 
	     1     4     7    10
	     2     5     8    11
	     3     6     9    12
	Y(:,:,5) = 
	     1     4     7    10
	     2     5     8    11
	     3     6     9    12
S = tensor(1:60,[3 4 5]); Y = scale(X,S,1:3) %<-- Scale in every mode.
Y is a tensor of size 3 x 4 x 5
	Y(:,:,1) = 
	     1     4     7    10
	     2     5     8    11
	     3     6     9    12
	Y(:,:,2) = 
	    13    16    19    22
	    14    17    20    23
	    15    18    21    24
	Y(:,:,3) = 
	    25    28    31    34
	    26    29    32    35
	    27    30    33    36
	Y(:,:,4) = 
	    37    40    43    46
	    38    41    44    47
	    39    42    45    48
	Y(:,:,5) = 
	    49    52    55    58
	    50    53    56    59
	    51    54    57    60
Y = S .* X %<-- Same as above.
Y is a tensor of size 3 x 4 x 5
	Y(:,:,1) = 
	     1     4     7    10
	     2     5     8    11
	     3     6     9    12
	Y(:,:,2) = 
	    13    16    19    22
	    14    17    20    23
	    15    18    21    24
	Y(:,:,3) = 
	    25    28    31    34
	    26    29    32    35
	    27    30    33    36
	Y(:,:,4) = 
	    37    40    43    46
	    38    41    44    47
	    39    42    45    48
	Y(:,:,5) = 
	    49    52    55    58
	    50    53    56    59
	    51    54    57    60

Scaling a sptensor in different modes

X = ones(sptenrand([3 4 5], 10)) %<-- Generate data.
X is a sparse tensor of size 3 x 4 x 5 with 10 nonzeros
	(1,2,2)     1
	(1,4,4)     1
	(2,1,4)     1
	(2,3,4)     1
	(2,4,3)     1
	(2,4,4)     1
	(2,4,5)     1
	(3,2,3)     1
	(3,3,2)     1
	(3,4,2)     1
S = 10 * [1:5]'; Y = scale(X,S,3) %<-- Scale in one mode.
Y is a sparse tensor of size 3 x 4 x 5 with 10 nonzeros
	(1,2,2)    20
	(1,4,4)    40
	(2,1,4)    40
	(2,3,4)    40
	(2,4,3)    30
	(2,4,4)    40
	(2,4,5)    50
	(3,2,3)    30
	(3,3,2)    20
	(3,4,2)    20
S = tensor(10 * [1:5]',5); Y = scale(X,S,3) %<-- Same as above.
Y is a sparse tensor of size 3 x 4 x 5 with 10 nonzeros
	(1,2,2)    20
	(1,4,4)    40
	(2,1,4)    40
	(2,3,4)    40
	(2,4,3)    30
	(2,4,4)    40
	(2,4,5)    50
	(3,2,3)    30
	(3,3,2)    20
	(3,4,2)    20
S = tensor(1:12,[3 4]); Y = scale(X,S,[1 2]) %<-- Scale in two modes.
Y is a sparse tensor of size 3 x 4 x 5 with 10 nonzeros
	(1,2,2)     4
	(1,4,4)    10
	(2,1,4)     2
	(2,3,4)     8
	(2,4,3)    11
	(2,4,4)    11
	(2,4,5)    11
	(3,2,3)     6
	(3,3,2)     9
	(3,4,2)    12
S = tensor(1:12,[3 4]); Y = scale(X,S,-3) %<-- Same as above.
Y is a sparse tensor of size 3 x 4 x 5 with 10 nonzeros
	(1,2,2)     4
	(1,4,4)    10
	(2,1,4)     2
	(2,3,4)     8
	(2,4,3)    11
	(2,4,4)    11
	(2,4,5)    11
	(3,2,3)     6
	(3,3,2)     9
	(3,4,2)    12
Z = scale(X,Y,1:3) %<-- Scale by a sparse tensor.
Z is a sparse tensor of size 3 x 4 x 5 with 10 nonzeros
	(1,2,2)     4
	(1,4,4)    10
	(2,1,4)     2
	(2,3,4)     8
	(2,4,3)    11
	(2,4,4)    11
	(2,4,5)    11
	(3,2,3)     6
	(3,3,2)     9
	(3,4,2)    12
X .* Y %<-- Same as above.
ans is a sparse tensor of size 3 x 4 x 5 with 10 nonzeros
	(1,2,2)     4
	(1,4,4)    10
	(2,1,4)     2
	(2,3,4)     8
	(2,4,3)    11
	(2,4,4)    11
	(2,4,5)    11
	(3,2,3)     6
	(3,3,2)     9
	(3,4,2)    12