From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Duplicate  matrix by a number of columns
Date: Sun, 15 May 2011 00:29:04 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 19
Message-ID: <iqn6og$pqe$>
References: <iqmb3e$8v1$> <iqmek0$ihv$> <iqmha6$pqb$>
Reply-To: <HIDDEN>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: 1305419344 26446 (15 May 2011 00:29:04 GMT)
NNTP-Posting-Date: Sun, 15 May 2011 00:29:04 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1187260
Xref: comp.soft-sys.matlab:726835

"Andy" wrote in message <iqmha6$pqb$>...
> 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