I am trying to get a matrix consisting of ordinary normalized orthogonal vectors based on an m-on-n-matrix, where m <n using Rcpp inside R. Unfortunately, I could not figure out how to use the qr method implemented in R to do this is.
My attempt closely follows the standard Gram-Schmidt orthonormalization algorithm, which was discussed here. However, the approach presented in this thread using Rcpp and Armadillo no longer works.
Matlab's implementation of the problem is provided by Gary Kuop in his collection of computer codes :
function [Q] = grams(A) [m,~] = size(A); Q=0*A; % compute QR using Gram-Schmidt for j = 1:m v = A(j,:); for i=1:j-1 v = v - Q(i,:)*A(j,:)'*Q(i,:); end Q(j,:) = v/norm(v); end
My current Rcpp code is as follows:
When modeling a random m-by-n matrix and applying an algorithm of errors, no errors arise. However, the resulting matrix, multiplied by its transposition, does not give a unit matrix, and I can not identify the error.
source share