gianni lavaredo
2012-Jul-02 11:38 UTC
[R] error to convert a Compute A^-1 B from Matlab to R using solve(A, B)
Dear Researchers, I need to convert the following equation in R from Matlab a = [x y ones(size(x))]; b = [-(x.^2+y.^2)]; a\b ans -9.9981 -16.4966 -7.6646 my solution in R is: a = cbind(x,y,rep(1,length(x))) b = cbind(-(x^2+y^2))> head(a)x y [1,] 14.45319 5.065726 1 [2,] 14.99478 5.173893 1 [3,] 14.64158 5.616916 1 [4,] 14.61803 6.624069 1 [5,] 14.19997 6.794587 1 [6,] 15.08174 8.224843 1> head(b)[,1] [1,] -234.5564 [2,] -251.6125 [3,] -245.9255 [4,] -257.5652 [5,] -247.8057 [6,] -295.1068 following MATLAB/ R Reference http://cran.r-project.org/doc/contrib/Hiebeler-matlabR.pdf the a\b could be converted by solve(a,b) but i get the following error:> solve(a,b)Error in solve.default(a, b) : 'b' must be compatible with 'a' thanks for any help Gianni [[alternative HTML version deleted]]
Jessica Streicher
2012-Jul-02 11:55 UTC
[R] error to convert a Compute A^-1 B from Matlab to R using solve(A, B)
Have a look at ?solve and see: a a square numeric or complex matrix containing the coefficients of the linear system. your a isn't square. The help also mentions qr.solve for non-square matrices. greetings Jessi On 02.07.2012, at 13:38, gianni lavaredo wrote:> Dear Researchers, > > I need to convert the following equation in R from Matlab > > a = [x y ones(size(x))]; > b = [-(x.^2+y.^2)]; > a\b > > ans > > -9.9981 > -16.4966 > -7.6646 > > my solution in R is: > > a = cbind(x,y,rep(1,length(x))) > b = cbind(-(x^2+y^2)) > >> head(a) > x y > [1,] 14.45319 5.065726 1 > [2,] 14.99478 5.173893 1 > [3,] 14.64158 5.616916 1 > [4,] 14.61803 6.624069 1 > [5,] 14.19997 6.794587 1 > [6,] 15.08174 8.224843 1 >> head(b) > [,1] > [1,] -234.5564 > [2,] -251.6125 > [3,] -245.9255 > [4,] -257.5652 > [5,] -247.8057 > [6,] -295.1068 > > following MATLAB/ R Reference > http://cran.r-project.org/doc/contrib/Hiebeler-matlabR.pdf > the a\b could be converted by solve(a,b) but i get the following error: > >> solve(a,b) > Error in solve.default(a, b) : 'b' must be compatible with 'a' > > thanks for any help > Gianni > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.[[alternative HTML version deleted]]