R and Octave/Matlab notes

As the Debian r-base package states: “S is the statistician’s Matlab and R is to S what Octave is to Matlab”. These notes document translations between R and Octave, and sometimes Matlab. (I do not have S and do not have access to Matlab all the time.)

Notes elsewhere

See the much more comprehensive notes on CRAN and on mathesaurus. This page just has a couple of things I have noted after working them out.

Data transfer

R uses the nice platform-independent XDR binary format for saving its workspace. I'm sure one could read this into Octave with a bit of effort. However, is probably easier to use HDF5, which is not the native format of either R and Octave, but is supported out of the box by both. In R do:


Then in octave

	load "filename.hdf"

works for simple data types, including matrices. Matlab should work too (it has a function called hdf5read), but I haven’t tried.

Sourcing a script file

In Octave/Matlab typing the name of a .m-file runs the commands as if typed into the current workspace. This has the same effect as source("filename.r",local=T,print.eval=T) in R.


Wall clock time

tic; ...; time_taken=toc


CPU time

start_time=cputime(); ...; cpu_time_taken=cputime()-start_time


To record start and stop times in R, call proc.time before and after the code. The difference between these vectors is what system.time returns.

The performance of R and Octave should be in the same ball-park for many things, although I have not done a careful comparison.