1

I'm converting a rather complicated set of code from Matlab to R. I have zero experience in Matlab and am a functioning novice in R.

I have a segment of code which reads (in matlab):

dSii=(sum(tao.*Sik,1))'-(sum(m'))'.*Sii-beta.*Sii./N.*(Iii+sum(Iik)');

Which I've simplified and will focus on the first segment (if I can solve the first segment I'm confident I can perform the rest):

J = (sum(A.*B,1))' - ...

tao (or A) and Sik (or B) are matrices. So my assumption is I'm performing matrix multiplication here (A * B)and summing the resultant column. The '1' is what is throwing me off in that statement. In R, that 1 would likely indicate we're talking about a sum of rows as opposed to columns(indicated by 2). But I can't find any supporting documentation for that kind of Matlab statement.

I was thinking of using a statement like this (but of course, too many '1's and ',')

J<- (apply(A*B, 1), 1, sum)

Thanks for all your help. I searched for other examples here and elsewhere and couldn't find an answer. I'm willing to work for it but this is akin to me studying French (which I don't know) to translate in Spanish (which I'm moderate in) while interpreting the whole process in English. :D

  • 1
    The dot operator is very meaningful in Matlab! Check out this question: http://stackoverflow.com/questions/27617166/how-to-translate-matlabs-dot-operators-into-r and the Rosetta-stones it links to. If you've just inherited the code, do you have "known good" inputs and outputs for it? (Too much to hope that you have actual unit and regression tests, maybe). – cphlewis Feb 18 '15 at 01:46
  • 1
    `A.*B` is *not* matrix multiplication! It is element-by-element multiplication. Say `A=[1 2 3]` and `B=[1 4 8]`, `A.*B=[1 8 24]`. Doing `sum(x,1)` is the same as `sum(x)`, it just sums each column. Typing `help sum` into Matlab, or googling "matlab sum" should take you to the documentation which will explain that. I don't know anything about R though! – David Feb 18 '15 at 02:07
  • There are worse ideas than trying to port code from one language you don't know to another language you don't know, but I can't think of any at the moment. Here, it looks like you don't even know whether the Matlab code is doing the task you want done. – Carl Witthoft Feb 18 '15 at 03:37
  • Thanks for the suggestions cphlewis and David. Carl Witthoft, I have nothing nice to say to you so... moving on. It is my mistake for writing matrix multiplication, as that has a very specific meaning. I understand that matlab interprets .* as element-wise multiplication of vectors. However, in my 'pre-work' I was using sites that recommended code snippets to go from Matlab to R and I came across this page http://mathesaurus.sourceforge.net/octave-r.html which succinctly says that Matlab operation a .* b is equivalent to R a * b. I'm still a bit stuck on the signficance of the '1' in matlab – Jake Hightower Feb 18 '15 at 03:59
  • It means you sum the rows of the matrix `A.*B`. –  Feb 18 '15 at 04:55

1 Answers1

1

Because of the different conventions in R and Matlab, the idiosyncrasies have to be learned for each (just like your language analogy!). The Matlab command sum(A.*B,1) means multiply A and B element-wise, so they must be the same shape, and then sum along dimension 1, i.e. add each row together to get the column sums. Dimension 1 is the default so, sum(A.*B) would do the same thing as sum(A.*B,1). Because R treats * as element-wise for matrix multiplication, the following Matlab and R codes will produce the same column of numbers in J:

Matlab:

A=[[1,2,3];[4,5,6];[7,8,9]];
B=[[10,11,12];[13,14,15];[16,17,18]];
J=sum(A.*B,1)';  %the ' means to transpose the column sums to be a 3x1 matrix

R:

A<-matrix(c(1,2,3,4,5,6,7,8,9),3,byrow=T)
B<-matrix(c(10,11,12,13,14,15,16,17,18),3,byrow=T)
J<-matrix(colSums(A*B)) # no transpose needed here: nrow(J)==3
chepyle
  • 976
  • 1
  • 9
  • 16