Convert Matlab Code to R

Converting matlab code to R code

Note

Broadly speaking, I agree with the expressed comments; however, I decided to vote up this question. (now deleted) This is due to the existence of matconv that facilitates this process.

Answer

Given your code, we could use matconv in the following manner:

pacman::p_load(matconv)
out <- mat2r(inMat = "input.m")

The created out object will attempt to translate Matlab code into R, however, the job is far from finished. If you inspect the out object you will see that it requires further work. Simple statements are usually translated correctly with Matlab comments % replaced with # and so forth but more complex statements may require a more detailed investigation. You could then inspect respective line and attempt to evaluate them to see where further work may be required, example:

eval(parse(text=out$rCode[1]))
NULL

(first line is a comment so the output is NULL)

Using tidyverse and matconv to convert Matlab code to R code

Try the following :

dir(path = ".", pattern = "*.m") %>%
purrr::map(.f = ~mat2r(
inMat = .x
, pathOutR = sub('m$', "R", .x)
, funcConverters = NULL
, dataConverters = NULL
, verbose = 1
)
)

How to convert MATLAB function into R function?

The inputs can be either numeric scalars or vectors. If you don't have Matlab you could install Octave which is free and compatible with Matlab and try the original Matlab code there and then compare the outputs for a test case to the result of running the following on the same test case to ensure that it gives the same result.

f <- function(aa, N, K, Ka, q) {
p <- N / K
lnqp <- ifelse(q == 0, log(p), (p^q - 1) / q)
Y <- (aa * (p * K / Ka - 1) - 1) * lnqp
Y
}

aa <- 1; N <- 1; K <- 1; Ka <- 1; q <- 1 # test data: change to use your data
f(aa, N, K, Ka, q)
## [1] 0

how to convert Matlab scripts in R

Rewriting from one language to another can be a painstaking process, especially because your have to take great care that the outcomes of both sets of codes are the same. I see roughly four approaches:

  • Digest the goal of the scripts, put aside the matlab code, and rewrite in R
  • Try and mimic the matlab code in R
  • Run the matlab code in octave, and interface with R
  • Run the code in Octave entirely

These are roughly in order of amount of work. If you just want to get the Matlab code working, definitely use Octave, which should run the code with minimal changes. If you want to convert the code to R, and continue developing in R, I would go for the first option. In that way you can leverage the real strenghts of R, as R is quite different (link with info, comparison R and matlab). But it does take the largest amount of time. Even if you reimplement in R, I would recommend getting the code running in Octave to be able to see if your results in R fit with the Matlab code.

Converting code from MATLAB to R

Matlab expression:

dA.*(A*N - N.*sum(A,2))

where

dA: real number
A: 10 x 10 matrix
N: 10 X 1 matrix
A*N: matrix multiplication
sum(A,2): sum of rows in A (10x1 matrix)
N.*sum(A,2): element by element multiplication (10 x 1 matrix)

Let's set up the following example in R:

A = matrix(data = 1:100,nrow = 10)
N = matrix(data = 1:10)
dA = 0.1

> A
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 1 11 21 31 41 51 61 71 81 91
[2,] 2 12 22 32 42 52 62 72 82 92
[3,] 3 13 23 33 43 53 63 73 83 93
[4,] 4 14 24 34 44 54 64 74 84 94
[5,] 5 15 25 35 45 55 65 75 85 95
[6,] 6 16 26 36 46 56 66 76 86 96
[7,] 7 17 27 37 47 57 67 77 87 97
[8,] 8 18 28 38 48 58 68 78 88 98
[9,] 9 19 29 39 49 59 69 79 89 99
[10,] 10 20 30 40 50 60 70 80 90 100

> N
[,1]
[1,] 1
[2,] 2
[3,] 3
[4,] 4
[5,] 5
[6,] 6
[7,] 7
[8,] 8
[9,] 9
[10,] 10

The first term is:

z1 = A %*% N

And the second term:

srow = rowSums(A)
z2 = srow * N

Which leads to the final result:

result = dA * (z1-z2)

Final equation

result = dA * (A %*% N - rowSums(A)*N)

This should give you the same answer as Matlab's dA.*(A*N - N.*sum(A,2))



Related Topics



Leave a reply



Submit