Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

How can I make the following line of code run faster? Is there a way to do this calculation as a matrix vs vector than vector vs vector?

In the code below, f is a matrix with one more row than vector. f has many columns and vector has only one column.

vector=RandomReal[1,100000];
f=RandomReal[1,{100001,520}];
pearson = Map[Correlation[f[[2 ;;, #]], vector] &, Range[5, Dimensions[f][[2]]]];
share|improve this question
add comment

1 Answer 1

up vote 3 down vote accepted

Let's separate sampling from the computation.

BlockRandom[SeedRandom[123];
 vector = RandomReal[1, 100000];
 f = RandomReal[1, {100001, 520}];
 ]

Here is doing it your way:

In[18]:= AbsoluteTiming[
 pearson2 = 
   Map[Correlation[f[[2 ;;, #]], vector] &, 
    Range[5, Dimensions[f][[2]]]];]

Out[18]= {5.865713, Null}

Of course you lose of standartization of vector. The solution is to use Correlation of two matrices:

In[19]:= AbsoluteTiming[
 pearson1 = Correlation[f[[2 ;;, 5 ;;]], Transpose[{vector}]];
 ]

Out[19]= {3.229262, Null}

Results are equal, of course:

In[20]:= Norm[pearson1 - pearson2]

Out[20]= 7.37439*10^-16
share|improve this answer
    
excellent.. that is exactly what I was looking for. thanks a lot! –  preeti Nov 22 '12 at 16:22
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.