R takes it. I prefer coding in R over MATLAB. I feel R understands that I do not like to type too much. A few examples:
In order to include a default for a parameter in own function we do:
In R
1 2 3 |
myfunction = function(x, DD=2 , TT) |
So myfunction asks to specify x and TT, which are left with no default while DD is left alone and will take the value 2 during execution unless otherwise specified. Not too complicated.
In Matlab
1 2 3 4 5 6 |
myfunction = function(x, TT, DD) % Note DD has to be last if nargin < 2 DD=2 end |
Many more characters. This becomes very tedious when I write functions to be used by others with many arguments which I want to have default values for.
Plotting is another example, see the difference:
1 2 3 4 5 6 |
In R: plot(x, ty = "p", col = 4) In Matlab plot(x, 'marker', 'o', 'col', 'b') |
13 characters in R, about twice as much in Matlab, and I plot A LOT. A workaround is to write a function with your favorite specifications.
When I would not mind to type some more for clarity, Matlab does not like it. Sometimes I have a function with many arguments, so I do not remember the order in which they are defined. R is flexible like that:
1 2 3 4 5 |
myfunction(x=c(1:10), DD=3 , TT=88) # Fine myfunction(TT=88, 3 , x=c(1:10) ) # Fine myfunction(c(1:10), 3 , 88 ) # Fine |
Matlab:
1 2 3 4 5 |
myfunction(x=c(1:10), DD=2 , TT=88) # NO myfunction(TT=88, 3 , x=c(1:10) ) # NO myfunction(c(1:10), 3 , 88 ) # Fine |
In short, Matlab is waiting for the input in the same order and is not willing take a named argument. This means I need to remember if TT is second or third, otherwise I need to open the function and have a look. If you have 10 arguments and a bad memory, in order to execute, you have to first go over the function to remember which argument sits where in the sequence. I am not aware of any workaround for this nuisance.
Interesting comparison, most of the time it feels indeed coding in R is somehow more comfortable. As someone switching from MATLAB I’m loving R so far.
By the way the MATLAB plot in your plotting example could be done by
plot(x, ‘bo’)
While introducing a bit of ambiguity…
Looking forward to the next head to head post!
You can specify color and markers in MATLAB with fewer characters if you want:
>> x = 1:10;
>> plot(x, ‘ob-‘); %where ‘o’ is the marker ‘b’ is the color, and ‘-‘ is the line type
>> plot(x, ‘xr:’); %In this case, ‘x’ markers, ‘r’ed color, and ‘:’ dashed lines
I agree with you on these issues with respect to Matlab. Python versus R on this one would be harder.