MATLAB Answers

Fly Haynes

How to output both rho and pvalues of the Corr function when having an iterative loop

Asked by Fly Haynes
on 21 Mar 2014
Latest activity Commented on by Fly Haynes
on 21 Mar 2014

Hi, I can't seem to get around how to do this. I have 12xm matrix (a1) of which I want to calculate the linear rank correlation rho an pvalues (using Corr function and pearson correlation type) from paired columns vectors, i.e. between 6 rows in each column and the next 6 rows in that column (these are replicate measurements, designated rep1 and rep2, respectively), and I want to iterate that with a for loop for all the m columns in the matrix a1.

I have it working so that it spits out the rho values in a 1xm vector, but I don't know how to get the script to do that also for the p-values associated with each correlation.

this is the script I have:

                  for repit = 1:size(a1,2);
                    results(repit) =  corr(rep1(:,repit),rep2(:,repit));

I know that [r,p] = corr(x,y) gives me both the r and p-value, but how do I build this into the iterative loop that I have going so that it does that for each each two column vectors in every columns of the a1 matrix.....?

I hope I am making this clear enough... otherwise, don't bother asking for clarifications.

Thank you.



No products are associated with this question.

1 Answer

Answer by Mischa Kim
on 21 Mar 2014
Edited by Mischa Kim
on 21 Mar 2014
 Accepted answer

Fly, you could use a cell array:

 [r{repit} p{repit}] = corr(rep1(:,repit),rep2(:,repit));

The results from the first iteration are then accessed via r{1} and p{1}, from the second in r{2}...

If the r- and p-values are scalars, you can also just use simple numeric arrays:

 [r(repit) p(repit)] = corr(rep1(:,repit),rep2(:,repit));

The advantage of cell arrays for non-scalars is that you don't need to worry about matrix dimensions.

  1 Comment


Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!