Comment Re:Show me some example code (Score 1) 382
Reply as AC below... error between chair and monitor
Reply as AC below... error between chair and monitor
Well, I haven't used MATLAB itself for a while - I do use Octave a bit though. So, try this
a = sin
In Octave I get
error: sin: too few arguments
instead i need to do
a = @sin
whereas the equivalent,
a <- sin
works in R. In other works, functions look just like other kinds of data in R, but a bit different in MATLAB (not much admittedly)
Another difference, I think, is the way that scope works. Example: a function which, given n, returns a function which increments n (Paul Graham's incrementor exercise)
f <- function(n){
function() {
n <<- n + 1
n
}
}
g <- f(2)
h <- f(-3)
g()
#returns 3
h()
#returns -2
g()
#returns 4
h()
#returns -1
Can you write this function, easily, in MATLAB?
Octave is great if you are moving from MATLAB to a free tool, because it has a similar syntax (IIRC it lags the MATLAB launguage by a couple of versions). But, in my view at any rate, R has a nicer language than either, and was well worth learning. You're right that R is stats oriented, but not some much that you feel crippled by it in use.
I use R a great deal. Think of it as an alternative to MATLAB, or Excel, rather than C or perl or lisp or whatever you like to use as a general purpose language. So, compared to MATLAB, functions are first class objects (rather like lisp), so, you can write functions that take functions as arguments, and return them as well, just as though
they were simple variables. It handles
vectors rather easily, and has decent plotting tools.
#quick example
# function, which, given numerical arguments a and b, and a function g, returns a function of x
f - function(a,b, g){
function(x){ a * x + g(b * x)}
}
f1 - f(1,2.5,sin)
x - seq(-pi,pi,l=100)
plot(x,f1(x),type='l')
The hardest part of climbing the ladder of success is getting through the crowd at the bottom.