# Generating the Leading Mode-n Vectors

The leading mode-n vectors are those vectors that span the subspace of the mode-n fibers. In other words, the left singular vectors of the n-mode matricization of X.

## Using nvecs to calculate the leading mode-n vectors

The nvecs command efficient computes the leading n-mode vectors.

```rand('state',0);
X = sptenrand([4,3,2],6) %<-- A sparse tensor
```
```X is a sparse tensor of size 4 x 3 x 2 with 6 nonzeros
(1,2,1)    0.8385
(2,3,1)    0.5681
(3,2,1)    0.3704
(3,3,1)    0.7027
(4,2,2)    0.5466
(4,3,2)    0.4449
```
```nvecs(X,1,2) %<-- The 2 leading mode-1 vectors
```
```ans =
0.5810    0.7687
0.3761   -0.5451
0.7219   -0.3347
0         0
```
```nvecs(X,1,3) % <-- The 3 leading mode-1 vectors
```
```ans =
0.5810    0.7687         0
0.3761   -0.5451         0
0.7219   -0.3347         0
0         0    1.0000
```
```nvecs(full(X),1,3) %<-- The same thing for a dense tensor
```
```ans =
0.5810    0.7687         0
0.3761   -0.5451         0
0.7219   -0.3347         0
0         0    1.0000
```
```X = ktensor({rand(3,2),rand(3,2),rand(2,2)}) %<-- A random ktensor
```
```X is a ktensor of size 3 x 3 x 2
X.lambda =
1     1
X.U{1} =
0.6946    0.9568
0.6213    0.5226
0.7948    0.8801
X.U{2} =
0.1730    0.2523
0.9797    0.8757
0.2714    0.7373
X.U{3} =
0.1365    0.8939
0.0118    0.1991
```
```nvecs(X,2,1) %<-- The 1 leading mode-2 vector
```
```ans =
0.2122
0.7739
0.5967
```
```nvecs(full(X),2,1) %<-- Same thing for a dense tensor
```
```ans =
0.2122
0.7739
0.5967
```
```X = ttensor(tenrand([2,2,2,2]),{rand(3,2),rand(3,2),rand(2,2),rand(2,2)}); %<-- A random ttensor
```
```nvecs(X,4,2) %<-- The 1 leading mode-2 vector
```
```ans =
0.7411   -0.6714
0.6714    0.7411
```
```nvecs(full(X),4,2) %<-- Same thing for a dense tensor
```
```ans =
0.7411   -0.6714
0.6714    0.7411
```

## Using nvecs for the HOSVD

```X = tenrand([4 3 2]) %<-- Generate data
```
```X is a tensor of size 4 x 3 x 2
X(:,:,1) =
0.2140    0.7266    0.4399
0.6435    0.4120    0.9334
0.3200    0.7446    0.6833
0.9601    0.2679    0.2126
X(:,:,2) =
0.8392    0.6072    0.4514
0.6288    0.6299    0.0439
0.1338    0.3705    0.0272
0.2071    0.5751    0.3127
```
```U1 = nvecs(X,1,4); %<-- Mode 1
U2 = nvecs(X,2,3); %<-- Mode 2
U3 = nvecs(X,3,2); %<-- Mode 3
S = ttm(X,{pinv(U1),pinv(U2),pinv(U3)}); %<-- Core
Y = ttensor(S,{U1,U2,U3}) %<-- HOSVD of X
```
```Y is a ttensor of size 4 x 3 x 2
Y.core is a tensor of size 4 x 3 x 2
Y.core(:,:,1) =
2.3791   -0.0479   -0.0295
0.0430    0.4256    0.0747
0.0387    0.1653   -0.0995
-0.0565    0.1769    0.2201
Y.core(:,:,2) =
0.1400    0.2010   -0.4216
-0.1373   -0.6148   -0.1499
0.4845   -0.0831    0.3278
0.1671    0.1620   -0.2217
Y.U{1} =
0.5386   -0.5134    0.6640   -0.0731
0.5965    0.0359   -0.3783    0.7070
0.4189   -0.2256   -0.5863   -0.6557
0.4227    0.8272    0.2687   -0.2548
Y.U{2} =
0.6086    0.7876    0.0967
0.6145   -0.3907   -0.6854
0.5020   -0.4765    0.7217
Y.U{3} =
0.8206   -0.5715
0.5715    0.8206
```
```norm(full(Y) - X) %<-- Reproduces the same result.
```
```ans =
1.1516e-15
```
```U1 = nvecs(X,1,2); %<-- Mode 1
U2 = nvecs(X,2,2); %<-- Mode 2
U3 = nvecs(X,3,2); %<-- Mode 3
S = ttm(X,{pinv(U1),pinv(U2),pinv(U3)}); %<-- Core
Y = ttensor(S,{U1,U2,U3}) %<-- Rank-(2,2,2) HOSVD approximation of X
```
```Y is a ttensor of size 4 x 3 x 2
Y.core is a tensor of size 2 x 2 x 2
Y.core(:,:,1) =
2.3791   -0.0479
0.0430    0.4256
Y.core(:,:,2) =
0.1400    0.2010
-0.1373   -0.6148
Y.U{1} =
0.5386   -0.5134
0.5965    0.0359
0.4189   -0.2256
0.4227    0.8272
Y.U{2} =
0.6086    0.7876
0.6145   -0.3907
0.5020   -0.4765
Y.U{3} =
0.8206   -0.5715
0.5715    0.8206
```
```100*(1-norm(full(Y)-X)/norm(X)) %<-- Percentage explained by approximation
```
```ans =
66.7999
```