[37] | 1 | function [error, Mapprox, stable1, stable2] = approximate(M, INC, r, NULLSPACE) |
---|
| 2 | |
---|
| 3 | stable1 = 0; |
---|
| 4 | stable2 = 0; |
---|
| 5 | sumnonzeros = (sum(NULLSPACE' ~= 0)')'; |
---|
| 6 | samprows = find(0 ~= sumnonzeros); |
---|
| 7 | nonsamprows = find(0 == sumnonzeros); |
---|
| 8 | |
---|
| 9 | numcols = size(M,2); |
---|
| 10 | |
---|
| 11 | [samp_approx,nonsampcols]=approx_matrix(M(samprows,:), INC(samprows,:), NULLSPACE(samprows,:),r,numcols); |
---|
| 12 | |
---|
| 13 | % The rows of NULLSPACE now contain all the nullspaces of the crossproduct spaces. |
---|
| 14 | % Use the nullspace to approximate M, taking r least principal components. |
---|
| 15 | if size(samp_approx,2) < r; |
---|
| 16 | error = -2; |
---|
| 17 | % samp_approx = -2 means that for some reason approximation wasn't possible. |
---|
| 18 | % or if < r columns, will not be able to extend the matrix correctly. |
---|
| 19 | else |
---|
| 20 | sampcols = setdiff(1:numcols, nonsampcols); |
---|
| 21 | [Mapprox, stable1] ... |
---|
| 22 | = extend_matrix(M(:,sampcols), INC(:,sampcols), samp_approx, samprows, nonsamprows, r); |
---|
| 23 | [Mapprox_transpose, stable2] = extend_matrix(M', INC', Mapprox', sampcols, nonsampcols, r); |
---|
| 24 | Mapprox = Mapprox_transpose'; |
---|
| 25 | |
---|
| 26 | % These extensions are necessary because the nullspace might not allow us to compute |
---|
| 27 | % every row of the approximating rank r linear space, and then this linear space might |
---|
| 28 | % not allow us to fill in some columns, if they are missing too much data. |
---|
| 29 | |
---|
| 30 | error = sum(sum((M.*INC - Mapprox.*INC).^2)); |
---|
| 31 | end |
---|
| 32 | |
---|
| 33 | |
---|