james.flood at fortis.com
2007-Mar-20 15:05 UTC
[R] Translating code from R into Matlab code
Hi, Correct me if I am wrong but can I translate the R code into Matlab via this package. ie I have a script in R, if I run this package on a Unix emulator can I get the R code displayed in Matlab format ( R code changed into Matlab code). If that is possible that would be great and if so how. Also, If this program cannot do this do you know one that can do this: Regards, James Flood -------------- next part -------------- --------------------------------------------------------------- This e-mail, including any attachments, contains information that is private and confidential and solely intended for the addressee or addressees. If you are not the named or intended recipient, please inform the sender immediately by reply transmission and delete this e-mail, including any attachments, without opening or copying it. In addition, in this event you are hereby notified that any disclosure, reading, reproduction, dissemination, distribution or any other use of this e-mail, including any attachments, is strictly prohibited. No warranty or guarantee is given with respect to the correct and accurate transmission of the content as well as with respect to the timely receipt of a sent e-mail. Messages and attachments are scanned for all viruses known. If this message contains password-protected attachments, the files have NOT been scanned for viruses by the Fortis.com mail domain. Always scan attachments before opening them. ---------------------------------------------------------------
On 20-Mar-07 15:05:27, james.flood at fortis.com wrote:> Hi, > > Correct me if I am wrong but can I translate the R code into > Matlab via this package. ie I have a script in R, if I run > this package on a Unix emulator can I get the R code displayed > in Matlab format ( R code changed into Matlab code). If that is > possible that would be great and if so how. Also, If this > program cannot do this do you know one that can do this: > > Regards, > James FloodEin program in R geschrieben ist nicht allgemein ins Matlab wortw?rtlich ?bersetzbar. A program in R written is not generally into Matlab word-wordly oversettable. An R program cannot in general be literally translated into Matlab. There are several differences in syntax between R and Matlab, R has a much richer class of data-structures than Matlab, and even individual operators can work in different ways in some circumstances. More importantly, an R function with any complexity will differ radically from the Matlab equivalent (and the return mechanism from functions is different). Even at the basic level, a Matlab vector is either a row-vector (a matrix with one row) or a column-vector (a matrix with one column) and the two are different. R's "vector" is basically a sequence of elements, and is neither "horizontal" nor "vertical". To convert it to a Matlab vector, you need to pass via a conversion to a matrix, and choose whether it is to be a row or a column -- e.g. x <- matrix(c(x1,x2,x3,x4,x5),nrow=1) While the two have enough in common for simple programs to be translated more-or-less literally, there is, in my view, no hope of an automatic translation from one to the other which one could trust. The way to go is as with any good translation between languages. First read and understand clearly what has been expressed in one language. Then use your skills in the other language to express that exact same meaning in the other language. The following is an example (in R and Octave) where the two are closely similar, but even so not quite (there are a few subtle differences which need hand-work). This is an implementation of the "Pool Adjacent Violators Algorithm" for monotonic regression, and in fact I wrote the R code by translating the Octave code (derived from Matlab code which I found on the Web). R CODE: pava<-function(x,wt=rep(1,length(x))) { n<-length(x) if(n<=1) return(x) lvlsets <- (1:n) repeat { viol<-(as.vector(diff(x))<0) if(!(any(viol))) break i <- min( (1:(n-1))[viol]) lvl1<-lvlsets[i] lvl2<-lvlsets[i+1] ilvl<-(lvlsets==lvl1 | lvlsets==lvl2) x[ilvl]<-sum(x[ilvl]*wt[ilvl])/sum(wt[ilvl]) lvlsets[ilvl]<-lvl1 } x } OCTAVE CODE: prefer_zero_one_indexing=1; n = max(size(x)); if(nargin==1), wt=ones(size(x)); endif lvlsets = (1:n)'; one2n = (1:n-1)'; while (true) viol = (x(1:(n-1))-x(2:n) > 0); if(!any(viol)), break; endif i = min(find(viol)); lvl1 = lvlsets(i) lvl2 = lvlsets(i+1); ilvl = (lvlsets==lvl1)|(lvlsets==lvl2); x(ilvl) = sum( x(ilvl).*wt(ilvl) )/sum(wt(ilvl)); lvlsets(ilvl) = lvl1; endwhile p = x; endfunction For a general overview of the relation between R and Matlab (strictly Octave, but the principles are the same) see the "R and Octave" refernce card at: http://cran.r-project.org/doc/contrib/R-and-octave.txt Hoping this helps, Ted. -------------------------------------------------------------------- E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk> Fax-to-email: +44 (0)870 094 0861 Date: 20-Mar-07 Time: 18:20:57 ------------------------------ XFMail ------------------------------
Apologies for my previous response appearing twice on the list. I first accidentally sent it from an address not subscribed to the list, so I thought it would not get through, and then re-sent it from the subscribed address. It seems both got to the list. Sorry. Ted. -------------------------------------------------------------------- E-Mail: (Ted Harding) <ted.harding at nessie.mcc.ac.uk> Fax-to-email: +44 (0)870 094 0861 Date: 20-Mar-07 Time: 19:11:24 ------------------------------ XFMail ------------------------------