# Collapsing and Scaling Tensors

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

## 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.5700    0.9278    0.8453
0.8867    0.6551    0.3683
0.9025    0.2050    0.1861
0.9622    0.2478    0.5658
X(:,:,2) =
0.6551    0.6102    0.2893
0.8380    0.8876    0.5269
0.0822    0.3187    0.7263
0.0506    0.5006    0.4659
```
```Y = collapse(X,[2 3]) %<-- Sum of entries in each mode-1 slice.
```
```Y is a tensor of size 4
Y(:) =
3.8977
4.1626
2.4208
2.7929
```
```Y = collapse(X,-1) %<-- Same as above.
```
```Y is a tensor of size 4
Y(:) =
3.8977
4.1626
2.4208
2.7929
```
```Z = collapse(X,2) %<-- Sum of entries in each row fiber.
```
```Z is a tensor of size 4 x 2
Z(:,:) =
2.3430    1.5546
1.9101    2.2525
1.2936    1.1272
1.7758    1.0171
```
```collapse(X,1:3) %<-- Sum of all entries.
```
```ans =

13.2740

```

## 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.9622
0.8876
```
```Z = collapse(X,-3,@mean) %<-- Average entry in each mode-3 slice.
```
```Z is a tensor of size 2
Z(:) =
0.6102
0.4960
```

## 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.9194
(1,3,1)    0.9742
(3,1,1)    0.9788
(4,2,1)    0.4579
(4,3,1)    0.1540
(4,3,2)    0.9585
```
```Y = collapse(X,[2 3]) %<-- Sum of entries in each mode-1 slice.
```
```Y =

1.8936
0
0.9788
1.5704

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

1.8936
0
0.9788
1.5704

```
```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.9742
(1,2)    0.9194
(3,1)    0.9788
(4,1)    0.6119
(4,2)    0.9585
```
```collapse(X,1:3) %<-- Sum of all entries.
```
```ans =

4.4428

```

## Alternate accumulation functions for sptensor

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

0.1540
0.9194

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

0.6412
0.9389

```

## 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,3,1)     1
(1,3,5)     1
(2,1,2)     1
(2,2,1)     1
(2,3,3)     1
(3,2,1)     1
(3,2,4)     1
(3,3,4)     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,3,1)    10
(1,3,5)    50
(2,1,2)    20
(2,2,1)    10
(2,3,3)    30
(3,2,1)    10
(3,2,4)    40
(3,3,4)    40
(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,3,1)    10
(1,3,5)    50
(2,1,2)    20
(2,2,1)    10
(2,3,3)    30
(3,2,1)    10
(3,2,4)    40
(3,3,4)    40
(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,3,1)     7
(1,3,5)     7
(2,1,2)     2
(2,2,1)     5
(2,3,3)     8
(3,2,1)     6
(3,2,4)     6
(3,3,4)     9
(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,3,1)     7
(1,3,5)     7
(2,1,2)     2
(2,2,1)     5
(2,3,3)     8
(3,2,1)     6
(3,2,4)     6
(3,3,4)     9
(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,3,1)     7
(1,3,5)     7
(2,1,2)     2
(2,2,1)     5
(2,3,3)     8
(3,2,1)     6
(3,2,4)     6
(3,3,4)     9
(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,3,1)     7
(1,3,5)     7
(2,1,2)     2
(2,2,1)     5
(2,3,3)     8
(3,2,1)     6
(3,2,4)     6
(3,3,4)     9
(3,4,3)    12
(3,4,5)    12
```