R和Matlab中的qr函数 [英] qr function in R and matlab

查看:885
本文介绍了R和Matlab中的qr函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于将matlab函数转换为R的问题,我希望有人可以提供帮助.

在matlab和R中使用的标准QR分解称为qr().据我了解,在两种语言中执行qr分解的标准方法是:

Matlab: [Q,R] = qr(A) 满足QR = A

R:

z <- qr(A)
Q <- qr.Q(z)
R <- qr.R(z)

不幸的是,这两者都为我提供了相同的结果,而这并不是我所需要的.我需要的是这样:

Matlab: [Q,R,e] = qr(A,0)会产生经济规模的分解,其中e是一个置换向量,因此A(:,e)= Q * R.

R: 没头绪

我尝试将 [Q,R,E] = qr(A)

进行比较

z <- qr(A);
Q <- qr.Q(z);
R <- qr.R(z);
E <- diag(ncol(A))[z$pivot]

,变量Q和E的结果似乎相同(但R的结果则不同).因此,根据定义的输入/输出,将得到不同的结果(这很有意义).

所以我的问题是: R中是否有一种方法可以在Matlab中模拟此[Q,R,e] = qr(A,0)?

我曾经尝试过研究matlab函数,但这导致了漫长而曲折的无穷函数定义之路,我希望有一个更好的解决方案.

任何帮助将不胜感激,如果我错过了明显的事情,我深表歉意.

解决方案

我认为差异在于计算的基础数字库.默认情况下,R的qr函数使用(很旧)的 LINPACK 例程,但是如果我做

z <- qr(X,LAPACK=T)

然后R使用LAPACK,结果似乎与MATLAB的结果相匹配(可能在下面也使用LAPACK).无论哪种方式,我们都可以看到与X的预期关系:

z <- qr(X,LAPACK=F)
all.equal(X[,z$pivot], qr.Q(z)%*%qr.R(z), check.attributes=FALSE)
# [1] TRUE

z <- qr(X,LAPACK=T)
all.equal(X[,z$pivot], qr.Q(z)%*%qr.R(z), check.attributes=FALSE)
# [1] TRUE

I have a question about converting a matlab function into R, and I was hoping that someone could help.

The standard QR decomposition used in both matlab and R is referred to as qr(). To my understanding, the standard way of performing a qr decomposition in both languages is:

Matlab: [Q,R] = qr(A) satisfying QR=A

R:

z <- qr(A)
Q <- qr.Q(z)
R <- qr.R(z)

Both of which provide me with the same results, unfortunately, this is not what I need. What I need is this:

Matlab: [Q,R,e] = qr(A,0) which produces an economy-size decomposition in which e is a permutation vector so that A(:,e) = Q*R.

R: No clue

I have tried comparing [Q,R,E] = qr(A) with

z <- qr(A);
Q <- qr.Q(z);
R <- qr.R(z);
E <- diag(ncol(A))[z$pivot]

and results seem identical for variables Q and E (but different for R). So depending on the defined inputs/outputs there will be different results (which makes sense).

So my question is: Is there a way in R that can mimic this [Q,R,e]=qr(A,0) in Matlab?

I have tried digging into the matlab function but it leads to a long and torturous road of endless function definitions and I was hoping for a better solution.

Any help would be much appreciated, and if I've missed something obvious, I apologize.

解决方案

I think the difference comes down to the numerical library underlying the calculations. By default, R's qr function uses the (very old) LINPACK routines, but if I do

z <- qr(X,LAPACK=T)

then R uses LAPACK and the results seem to match MATLAB's (which is probably also using LAPACK underneath). Either way we see the expected relationship with X:

z <- qr(X,LAPACK=F)
all.equal(X[,z$pivot], qr.Q(z)%*%qr.R(z), check.attributes=FALSE)
# [1] TRUE

z <- qr(X,LAPACK=T)
all.equal(X[,z$pivot], qr.Q(z)%*%qr.R(z), check.attributes=FALSE)
# [1] TRUE

这篇关于R和Matlab中的qr函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆