-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathexample2_extract.m
44 lines (42 loc) · 1.16 KB
/
example2_extract.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
function [U,V] = example2_extract(m,n,k,x)
%EXAMPLE2_EXTRACT Matrix factorization example: extraction.
%
% Helper function to extract factors of an approximate
% (rank-reduced) two-factor decomposition of a matrix:
%
% A \approx U*V'
%
% Input
% m: number of rows of U to be approximated
% n: number of rows of V to be approximated
% k: rank of approximation factors
% x: vector of variables in two-factor approximation
%
% Output
% U: first factor
% V: second factor
%
% Matrix dimensions
% A is m x n
% U is m x k
% V is n x k
%
% Example
% m = 3; n = 2; k = 2;
% [x,Data] = example2_init(m,n,k);
% [U,V] = example_extract(m,n,k,x)
% norm(Data.A-U*V')
%
%Poblano Toolbox for MATLAB
%
%Copyright 2009 National Technology & Engineering Solutions of Sandia,
%LLC (NTESS). Under the terms of Contract DE-NA0003525 with NTESS, the
%U.S. Government retains certain rights in this software.
%% Perform check on sizes of inputs
[x_m, x_n] = size(x);
if ( (x_n ~= 1) || (x_m ~= (m+n)*k) )
error('Dimensions do not agree!');
end
%% Extract approximation factors from x
U = reshape(x(1:m*k),m,k);
V = reshape(x(m*k+1:m*k+n*k),n,k);