| Octave | R | Comments |
3+4i | 3+4i | |
i | 1i | R treats “i” as a variable name |
abs(3+4i) | Mod(3+4i) | but put a numeral in front |
arg(3+4i) | Arg(3+4i) | |
conj(3+4i) | Conj(3+4i) | get these from help(Mod) |
real(3+4i) | Re(3+4i) | |
imag(3+4i) | Im(3+4i) | |
sqrt(-2) | sqrt(-2+0i) | R has explicit complex arithmetic |
| Octave | R |
1:10 | 1:10 or seq(10) |
1:3:10 | seq(1,10,by=3) |
10:-1:1 | 10:1 |
10:-3:1 | seq(from=10,to=1,by= -3) |
linspace(1,10,7) | seq(1,10,length=7) |
(1:10)+i | 1:10+1i |
a(1:end) | a[1:length(a)] |
| Octave | R | comments |
a=[2 3 4 5]; | a <- c(2,3,4,5) | R output not echoed to the screen if assigned |
a=[2 3 4 5] | (a <- c(2,3,4,5)) | (but round brackets around an expression force echo) |
adash=[2 3 4 5]’ ; | adash <- t(c(2,3,4,5)) |
[a a] | c(a,a) |
[a a*3] | c(a,a*3) |
a.*a | a*a |
a.^3 | a^3 |
[1:4 a] | c(1:4,a) |
| Octave | R |
[1:4 1:4] | rep(1:4,2) |
repmat(1:4,1,3) | rep(1:4,3) |
repmat(1:4,3,1) | kronecker(matrix(1,3,1),t(1:4)) |
nonzeros(triu(repmat(1:4,4,1)))’ | rep(1:4,1:4) |
repmat(1:4,3,1)(:)’ | rep(1:4,each=3) |
In R, the negative indexing means that the elements are not wanted.
| Octave | R | |
a=1:100; | a <- 1:100 |
a(2:100) | a[-1] | omit the first element. |
a([1:9 11:100]) | a[-10] | omit the tenth element. |
a([1:9 12:100]) | a[-(10:11)] | need parentheses: “:” operator has low priority |
a([find(mod(0:49,3)), 51:end]) | a[-seq(1,50,3)] | omit 1,4,7,... |
Destructive modification:
| Octave | R | |
a(1) = [] | a=a[-1] | remove the first element. |
a(10) = [] | a=a[-10] | remove the tenth element. |
a(1:3:50) = [] | a=a[-seq(1,50,3)] | remove 1,4,7,... |
| Octave | R |
(1:3)[true false true] | (1:3)[c(TRUE,FALSE,TRUE)] |
a(a>90)= -44; | a[a>90] <- -44 |
find(a>90) | which(a>90) |
| Octave | R |
[1:4 ; 1:4] | rbind(1:4,1:4) |
[1:4 ; 1:4]’ | cbind(1:4,1:4) or t(rbind(1:4,1:4)) |
[2 3 4 5] | c(2,3,4,5) |
[2 3;4 5] | rbind(c(2,3),c(4,5)) rbind() binds rows |
[2 3;4 5]’ | cbind(c(2,3),c(4,5)) or matrix(2:5,2,2) |
a=[5 6]; | a <- c(5,6) |
b=[a a;a a]; | b <- rbind(c(a,a),c(a,a)) see repmat below |
[1:3 1:3 1:3 ; 1:9] | rbind(1:3, 1:9) |
[1:3 1:3 1:3 ; 1:9]’ | cbind(1:3, 1:9) |
[repmat(1:3,1,8)(1:8);1:8] | rbind(1:3, 1:8) |
Debugging
| Matlab | R | Comments |
| keyboard | browser() ; debug(“function_name”) | to exit debugging mode see below |
| return | Q | Halt execution and drop to top level see Here, chapter 9 (Debugging) |
| ans | .Last.value | |
| disp(44) | print(44) | |
Plotting
| Matlab | R | Comments |
| a=rand(10) | a <- array(runif(100),c(10,10)) or matrix(runif(100),nrow=10) | |
| help plot | help (plot) and methods(plot) | |
| plot(a) | matplot(a,type=”l”,lty=1) | |
| plot(a,’r’) | matplot(a,type=”l”,lty=1,col=”red”) | |
| plot(a,’x’) | matplot(a,pch=4) | see example(“points”) for the symbols |
| plot(a,’–‘) | matplot(a,type=”l”,lty=2) | |
| plot(a,’x-’) | matplot(a,pch=4,type=”b”,lty=1) | |
| plot(a,’x–‘) | matplot(a,pch=4,type=”b”,lty=2) | |
| semilogy(a) | matplot(a,type=”l”,lty=1,log=”y”) | |
| semilogx(a) | matplot(a,type=”l”,lty=1,log=”x”) | |
| loglog(a) | matplot(a,type=”l”,lty=1,log=”xy”) | |
| plot(1:10,’r’), hold on, plot(10:-1:1,’b’) | plot(1:10,col=”red”,type=”l”); matplot(10:1,col=”blue”,type=”l”,add=TRUE) or matlines(10:1,col=”blue”) | |
| plot([1:10 10:-1:1]), axis equal | ? not sure ? maybe MASS::eqscplot(1:10,10:1) | |
to be continued
Based on a reference sheet translating between the most common Octave (or Matlab) and R commands provided by Robin Hankin.
Also see Dave Hiebeler’s extensive reference guide
— Romain François 2006/03/04
— Kath 2009/06/10