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.9473    0.6743    0.6155
	    0.8133    0.9271    0.0034
	    0.9238    0.3438    0.9820
	    0.1990    0.5945    0.8995
	X(:,:,2) = 
	    0.6928    0.2999    0.0974
	    0.4397    0.8560    0.3974
	    0.7010    0.1121    0.3333
	    0.6097    0.2916    0.9442
Y = collapse(X,[2 3]) %<-- Sum of entries in each mode-1 slice.
Y is a tensor of size 4
	Y(:) = 
	    3.3272
	    3.4369
	    3.3961
	    3.5385
Y = collapse(X,-1) %<-- Same as above.
Y is a tensor of size 4
	Y(:) = 
	    3.3272
	    3.4369
	    3.3961
	    3.5385
Z = collapse(X,2) %<-- Sum of entries in each row fiber.
Z is a tensor of size 4 x 2
	Z(:,:) = 
	    2.2371    1.0901
	    1.7438    1.6931
	    2.2497    1.1464
	    1.6930    1.8455
collapse(X,1:3) %<-- Sum of all entries.
ans =

   13.6987

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.9820
	    0.9442
Z = collapse(X,-3,@mean) %<-- Average entry in each mode-3 slice.
Z is a tensor of size 2
	Z(:) = 
	    0.6603
	    0.4813

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,1,2)    0.7221
	(1,3,1)    0.9685
	(2,1,1)    0.1557
	(4,1,1)    0.1630
	(4,2,1)    0.3134
	(4,2,2)    0.0294
Y = collapse(X,[2 3]) %<-- Sum of entries in each mode-1 slice.
Y =

    1.6906
    0.1557
         0
    0.5057

Y = collapse(X,-1) %<-- Same as above.
Y =

    1.6906
    0.1557
         0
    0.5057

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.9685
	(1,2)    0.7221
	(2,1)    0.1557
	(4,1)    0.4764
	(4,2)    0.0294
collapse(X,1:3) %<-- Sum of all entries.
ans =

    2.3520

Alternate accumulation functions for sptensor

Y = collapse(X,[1 2],@min) %<-- Min *nonzero* entry in each mode-3 slice.
Y =

    0.1557
    0.0294

Z = collapse(X,-3,@mean) %<-- Average *nonzero* entry in each mode-3 slice.
Z =

    0.4001
    0.3758

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,1,1)     1
	(1,1,4)     1
	(1,2,4)     1
	(1,3,5)     1
	(1,4,4)     1
	(2,1,4)     1
	(2,3,5)     1
	(3,1,2)     1
	(3,4,3)     1
	(3,4,5)     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,1,1)    10
	(1,1,4)    40
	(1,2,4)    40
	(1,3,5)    50
	(1,4,4)    40
	(2,1,4)    40
	(2,3,5)    50
	(3,1,2)    20
	(3,4,3)    30
	(3,4,5)    50
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,1,1)    10
	(1,1,4)    40
	(1,2,4)    40
	(1,3,5)    50
	(1,4,4)    40
	(2,1,4)    40
	(2,3,5)    50
	(3,1,2)    20
	(3,4,3)    30
	(3,4,5)    50
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,1,1)     1
	(1,1,4)     1
	(1,2,4)     4
	(1,3,5)     7
	(1,4,4)    10
	(2,1,4)     2
	(2,3,5)     8
	(3,1,2)     3
	(3,4,3)    12
	(3,4,5)    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,1,1)     1
	(1,1,4)     1
	(1,2,4)     4
	(1,3,5)     7
	(1,4,4)    10
	(2,1,4)     2
	(2,3,5)     8
	(3,1,2)     3
	(3,4,3)    12
	(3,4,5)    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,1,1)     1
	(1,1,4)     1
	(1,2,4)     4
	(1,3,5)     7
	(1,4,4)    10
	(2,1,4)     2
	(2,3,5)     8
	(3,1,2)     3
	(3,4,3)    12
	(3,4,5)    12
X .* Y %<-- Same as above.
ans is a sparse tensor of size 3 x 4 x 5 with 10 nonzeros
	(1,1,1)     1
	(1,1,4)     1
	(1,2,4)     4
	(1,3,5)     7
	(1,4,4)    10
	(2,1,4)     2
	(2,3,5)     8
	(3,1,2)     3
	(3,4,3)    12
	(3,4,5)    12