[R] Translating code from R into Matlab code

(Ted Harding) ted.harding at nessie.mcc.ac.uk
Tue Mar 20 19:21:26 CET 2007


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 Flood

  Ein 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 ------------------------------



More information about the R-help mailing list