Path: news.mathworks.com!not-for-mail 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$1@newscl01ah.mathworks.com> References: <iqmb3e$8v1$1@newscl01ah.mathworks.com> <iqmek0$ihv$1@newscl01ah.mathworks.com> <iqmha6$pqb$1@newscl01ah.mathworks.com> Reply-To: <HIDDEN> NNTP-Posting-Host: www-00-blr.mathworks.com Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Trace: newscl01ah.mathworks.com 1305419344 26446 172.30.248.45 (15 May 2011 00:29:04 GMT) X-Complaints-To: news@mathworks.com NNTP-Posting-Date: Sun, 15 May 2011 00:29:04 +0000 (UTC) X-Newsreader: MATLAB Central Newsreader 1187260 Xref: news.mathworks.com comp.soft-sys.matlab:726835 "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