Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert MATLAB code to R [closed]

Tags:

r

matlab

Is there a tool for converting MATLAB code to R?

I have a lot of code that needs to be converted from MATLAB to R. It doesn't have to be accurate, but it will be helpful in giving a head start.

like image 488
user236215 Avatar asked Apr 03 '11 02:04

user236215


People also ask

How do I convert Matlab code to R?

If you just want a basic conversion with R syntax, calling “mat2r” with just the Matlab code. The function outputs a list with the converted code as “rCode” and the original code as “matCode”. This is so you can easily look at the differences between the two and what exactly the program is doing.

Can Matlab code run in R?

Option 2: Use R. R. matlab is a package that communicates with MATLAB, can read and write MAT files, and can pass objects to (and receive objects from) MATLAB. It's a little tricky to use at first, but the ability to pass objects between R and MATLAB can be very useful.


1 Answers

Paul Gilbert provides a rough Bash script that could get you started (he claims it will convert about 80% of the way) on the R mailing list:

#!/bin/csh cp $1 $2 ex -s $2 <<eof    g/%/s//#/g    g/function\(..*\)=\(..*\)(\(..*\)/s//\2 <-function( \3 { \1/    g/end/s//   } #/    g/for\(..*\)=\(..*\):\(..*\)/s//for ( \1 in \2 : \3 ) {/    g/_/s//./g    g/;/s///g    g/==/s//@@/g    g/=/s//<-/g    g/@@/s//==/g    g/zeros(/s//matrix(0,/g    g/ones(/s//matrix(1,/g    g/eye(/s//diag(1,/g    g/\/s//solve(,)/g    g/fsolve('\(..*\)'/s//ms(~\1 /g    g/param(\(..*\))/s//param[ \1 ] /g    g/var(\(..*\))/s//var[ \1 ] /g    g/mod1(\(..*\)/s//mod1[ \1 /g    wq eof 
like image 176
Abe Avatar answered Sep 24 '22 22:09

Abe