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

Thread Subject:
Duplicate matrix by a number of columns

Subject: Duplicate matrix by a number of columns

From: Andy

Date: 14 May, 2011 16:37:02

Message: 1 of 5

I am a Matlab newbie and in need help getting some stuff done. My problem is that I A size(106,20503) and matrix B of size (106,1). I want to determine the correlation coefficient of matrix B and all the columns in matrix A. I am not getting any success because the two matrices have different sizes. My idea is to duplicate the columns in matrix B to have the same size as A (106, 20503) whiles keeping the individual elements the same.

Any ideas how to do this?

Thanks in advance

~ Andy

Subject: Duplicate matrix by a number of columns

From: Roger Stafford

Date: 14 May, 2011 17:37:04

Message: 2 of 5

"Andy" wrote in message <iqmb3e$8v1$1@newscl01ah.mathworks.com>...
> I am a Matlab newbie and in need help getting some stuff done. My problem is that I A size(106,20503) and matrix B of size (106,1). I want to determine the correlation coefficient of matrix B and all the columns in matrix A. I am not getting any success because the two matrices have different sizes. My idea is to duplicate the columns in matrix B to have the same size as A (106, 20503) whiles keeping the individual elements the same.
>
> Any ideas how to do this?
>
> Thanks in advance
>
> ~ Andy
- - - - - - - - - - - -
  Why not devise your own correlation computation? It's simple enough. Subtract the mean value of each column of A and of B itself from each of their respective elements. Then divide the resulting elements by the square root of the sum of the squares of the respective columns. Finally you can obtain the correlation by simple matrix multiplication of the resulting revised A2'*B2 to get correlation in a 20503 by 1 column vector. The 'bsxfun' function is useful in doing the above.

Roger Stafford

Subject: Duplicate matrix by a number of columns

From: Greg Heath

Date: 14 May, 2011 17:43:12

Message: 3 of 5

On May 14, 1:37 pm, "Roger Stafford"
<ellieandrogerxy...@mindspring.com.invalid> wrote:
> "Andy" wrote in message <iqmb3e$8v...@newscl01ah.mathworks.com>...
> > I am a Matlab newbie and  in need help getting some stuff done. My problem is that I A size(106,20503) and matrix B of size (106,1). I want to determine the correlation coefficient of matrix B and all the columns in matrix A. I am not getting any success because the two matrices have different sizes. My idea is to duplicate the columns in matrix B to have the same size as A (106, 20503) whiles keeping the individual elements the same.
>
> > Any ideas how to do this?
>
> > Thanks in advance
>
> > ~ Andy
>
> - - - - - - - - - - - -
>   Why not devise your own correlation computation?  It's simple enough.  Subtract the mean value of each column of A and of B itself from each of their respective elements.  Then divide the resulting elements by the square root of the sum of the squares of the >respective columns.  

help zscore
doc zscore

>Finally you can obtain the correlation by simple matrix multiplication of the resulting revised A2'*B2 to get correlation in a 20503 by 1 column vector.  The 'bsxfun' function is useful in doing >the above.

Hope this helps

Greg

Subject: Duplicate matrix by a number of columns

From: Andy

Date: 14 May, 2011 18:23:02

Message: 4 of 5

"Roger Stafford" wrote in message <iqmek0$ihv$1@newscl01ah.mathworks.com>...
> "Andy" wrote in message <iqmb3e$8v1$1@newscl01ah.mathworks.com>...
> > I am a Matlab newbie and in need help getting some stuff done. My problem is that I A size(106,20503) and matrix B of size (106,1). I want to determine the correlation coefficient of matrix B and all the columns in matrix A. I am not getting any success because the two matrices have different sizes. My idea is to duplicate the columns in matrix B to have the same size as A (106, 20503) whiles keeping the individual elements the same.
> >
> > Any ideas how to do this?
> >
> > Thanks in advance
> >
> > ~ Andy
> - - - - - - - - - - - -
> Why not devise your own correlation computation? It's simple enough. Subtract the mean value of each column of A and of B itself from each of their respective elements. Then divide the resulting elements by the square root of the sum of the squares of the respective columns. Finally you can obtain the correlation by simple matrix multiplication of the resulting revised A2'*B2 to get correlation in a 20503 by 1 column vector. The 'bsxfun' function is useful in doing the above.
>
> Roger Stafford

Thank you Roger. As I said I am a little bit new to matlab. I am looking into how to use the bsxfun.

Andy

Subject: Duplicate matrix by a number of columns

From: Roger Stafford

Date: 15 May, 2011 00:29:04

Message: 5 of 5

"Andy" wrote in message <iqmha6$pqb$1@newscl01ah.mathworks.com>...
> Thank you Roger. As I said I am a little bit new to matlab. I am looking into how to use the bsxfun.
> Andy
- - - - - - - - - - -
  The following carries out the steps I described and gives C, a column vector in which each element is the correlation of the corresponding column of A with B. See if you can figure out what 'bsxfun' is doing in the middle two lines.

 T = [A,B];
 T = bsxfun(@minus,T,mean(T,1));
 T = bsxfun(@rdivide,T,sqrt(sum(T.^2,1)));
 C = T(:,1:end-1).'*T(:,end);

  You can check this by computing

 C2 = corrcoef([A,B]);
 C2 = C2(1:end-1,end);

and comparing C with C2. However the latter method involves a lot of unnecessary computation (in spite of the shorter code) and is surely slower.

Roger Stafford

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us