R vs. Matlab – a small example

15Feb10

At the institute I’m working quite a lot of people prefer using Matlab and only a few of them know about R. Today one of my colleagues — who is also an eager user of Matlab — ran into the following problem:

  • He had a vector v in hand which consisted of \frac{n(n+1)}{2} elements.
  • He wanted to reshape this data into an n×n matrix M, where the element M_{ij} is equal to v_{k+j}I(j<=n-i+1) with k=\frac{(2n-i+2)(i-1)}{2} and I(j <= n-i+1)=1 if the condition j <= n-i+1 is satisfied and 0 otherwise. In other words, the first (n-i+1)th element of the ith row of M is equal to the vector (v_{k+1},v_{k+2},\ldots,v_{k+n-i+1}) and the remaining elements are zero.

He struggled for long minutes of how he should design a loop for doing this task. Of course writing such a loop is not a highly difficult task, but why would we waste our time, if we can get the same result in a single line of R code?

For the sake of illustration, I’ve generated an input vector for the case of n=99 (the value of n was 99 in my colleague’s problem as well):

v <- rep(99:1,times=99:1)

and used the one-liner

M <- t(matrix(unlist(tapply(v,rep(1:99,times=99:1),function(x) c(x,rep(0,99-length(x))))),nrow=99))

This is the kind of compactness I like pretty much in R. At the end I would like to emphasize that this post is not against Matlab, it just points out how the different logic of the R language can simplify problem solving in many situations. As a bonus let me share the visualization of the resulted matrix using the color2D.matplot function of the plotrix package:

library(plotrix)
color2D.matplot(M,c(0,1),c(1,0),c(0,0))



13 Responses to “R vs. Matlab – a small example”

  1. 1 nmwe

    I’ve always wondered why Matlab despite having so much syntactic sugar for working with matrices, often forces you to resort to loops for cases such as the above. I might have missed out some stuff and have searched the manual more than once if there were any easier ways to do things but have yet to find it.

  2. Nice result. MATLAB and the SAS/IML language are similar languages. If your collegue hasn’t solved the problem yet, I wrote a SAS/IML solution at
    http://blogs.sas.com/iml/index.php?/archives/3-Filling-an-Upper-Triangular-Matrix-from-a-Vector.html

  3. 3 bub

    I have two questions:

    - how much longer are ‘long’ minutes than normal minutes? :o) just kidding…but I would probably have struggled days or weeks..

    - should I use matlab or R, are they mutually exclusive in any way?

    Thanks.

  4. 4 Atul

    Check out, looks much simpler than the loop suggested by you:
    sparse(i,j,s,m,n)

  5. 5 Atul

    To be more specific:

    [i,j]=find(triu(ones(n)))
    M=full(sparse(i,j,v,n,n))

    and you are done. Way more compact.

  6. 6 Saurabh Chandorkar

    Very occasions in MATLAB require loops. I can always write a single or two line code to do what you want without resorting to loops. In short, even moderately advanced MATLAB user will know never to touch a loop! I am no expert in R, but I can do what you need in a couple of MATLAB code lines. No problem at all.

    But, your post at least suggests that R can do what I can do in MATLAB in just a couple of lines of compact code. Thanks.

  7. 7 miguel

    In Matlab! ;)
    For example, your vector for some n:
    n = 5;
    Your vector:
    v = 1:n*(n+1)/2;
    An your matrix
    M = fliplr(triu(ones(n)));
    M(logical(M)) = v;
    Ciao

  8. 8 Xiaolong

    m=zeros(n);
    m(logical(triu(ones(n))))=v;

  9. 9 Michael

    Is there any point in replying to such an old thread?

    I’d do:

    j=matrix(1:n,n,n); i=t(j)
    k=(2*n-i+2)*(i-1)/2
    M=matrix(0,n,n)
    M[j <= n-i+1]=v[(k+j)[j <= n-i+1]]

    Not so compact, but I like to be clear….

  10. 10 mjm

    How do you do RREF on a matrix in R? I think matlab does it out of the box. In R, it seems that you’ve to write your own function!


  1. 1 The DO Loop
  2. 2 Programming Languages: R versus Matlab for quantitative finance? - Quora
  3. 3 Filling an Upper Triangular Matrix from a Vector - The DO Loop

Follow

Get every new post delivered to your Inbox.

Join 48 other followers

%d bloggers like this: