Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8077613
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T15:31:24+00:00 2026-06-05T15:31:24+00:00

Really confused why the QR output using RcppArmadillo is different than QR output from

  • 0

Really confused why the QR output using RcppArmadillo is different than QR output from R; Armadillo documentation doesnt give a clear answer either. Essentially when I give R a matrix Y that is n * q (say 1000 X 20 ) , I get back Q which is 1000 X 20 and R 20 X 1000. This is what I need. But when I use the QR solver in Armadillo, it throws me back Q 1000 X 1000 and R
1000 X 20. Can I call R’s qr function instead? I need Q to have dimension n x q, not q x q. Code below is what I am using(its a part of a bigger function).

If someone can suggest how to do it in RcppEigen, that’d be helpful too.

library(inline)
library(RcppArmadillo)

src <- '
    Rcpp::NumericMatrix Xr(Xs);
    int q = Rcpp::as<int>(ys);

    int n = Xr.nrow(), k = Xr.ncol();
    arma::mat X(Xr.begin(), n, k, false);

    arma::mat G, Y, B;

    G = arma::randn(n,q);

    Y = X*G;

    arma::mat Q, R;
    arma::qr(Q,R,Y);

    return Rcpp::List::create(Rcpp::Named("Q")=Q,Rcpp::Named("R")=R,Rcpp::Named("Y")=Y);'


rsvd <- cxxfunction(signature(Xs="numeric", ys="integer"), body=src, plugin="RcppArmadillo")
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T15:31:26+00:00Added an answer on June 5, 2026 at 3:31 pm

    (NOTE: This answer explains why R and RcppArmadillo return matrices with different dimensions, but not how to make RcppArmadillo return the 1000*20 matrix that R does. For that, perhaps take a look at the strategy used near the bottom of the qr.Q() function definition.)


    R’s qr() function does not return Q directly. For that, you need to use qr.Q(), like this:

    m <- matrix(rnorm(10), ncol=2)
    qr.Q(qr(m))
    #             [,1]        [,2]
    # [1,] -0.40909444  0.05243591
    # [2,]  0.08334031 -0.07158896
    # [3,]  0.38411959 -0.83459079
    # [4,] -0.69953918 -0.53945738
    # [5,] -0.43450340  0.06759767
    

    Notice that qr.Q() returns a matrix of the the same dimension as m, rather than a full 5*5 Q matrix. You can use the complete= argument to control this behavior, and get a Q matrix of full dimension:

    qr.Q(qr(m), complete=TRUE)
    #             [,1]        [,2]       [,3]       [,4]        [,5]
    # [1,] -0.40909444  0.05243591  0.3603937 -0.7158951 -0.43301590
    # [2,]  0.08334031 -0.07158896 -0.8416121 -0.5231477  0.07703927
    # [3,]  0.38411959 -0.83459079  0.2720003 -0.2389826  0.15752300
    # [4,] -0.69953918 -0.53945738 -0.2552198  0.3453161 -0.18775072
    # [5,] -0.43450340  0.06759767  0.1506125 -0.1935326  0.86400136
    

    In your case, it sounds like RcppArmadillo is returning the full 1000×1000 Q matrix (like qr.Q(q(m, complete=FALSE)) would), rather than just its 1st 20 columns (like qr.Q(q(m)) would).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have really been confused by this one. After pulling from a repo, I
I'm really confused. I'm using Django 1.4 and I've been searching for this the
I am really confused about object relationships! I have two classes Person and Address.
I'm really confused by a behaviour of LINQ I'm seeing and it's causing me
I am really confused now on how and which method to use to return
I'm really confused about the visitor pattern and its uses. I can't really seem
I'm really confused with an array im making, its gotten a bit confusing to
i'm really confused how to name method names in Ruby classes. if i create
I'm really confused. I want to make a Chrome Extension that can update a
I am getting really confused with this concept: If I want to implement a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.