### R vs. Matlab – a small example

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 in hand which consisted of elements.
- He wanted to reshape this data into an n×n matrix , where the element is equal to with and if the condition is satisfied and otherwise. In other words, the first th element of the th row of is equal to the vector 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 (the value of 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))

Filed under: R / R-Code | 13 Comments

Tags: Matlab, plotrix, R

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.

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

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.

Check out, looks much simpler than the loop suggested by you:

sparse(i,j,s,m,n)

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.

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.

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

m=zeros(n);

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

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….

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!