Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Avoiding loops in a symmetric matrix.
Date: Fri, 5 Dec 2008 21:46:01 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 37
Message-ID: <ghc7ep$laq$1@fred.mathworks.com>
References: <ghbdre$ahh$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-02-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1228513561 21850 172.30.248.37 (5 Dec 2008 21:46:01 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Fri, 5 Dec 2008 21:46:01 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1187260
Xref: news.mathworks.com comp.soft-sys.matlab:505296

"Jose " <jose.l.vega@gmail.com> wrote in message <ghbdre$ahh$1@fred.mathworks.com>...
> Hi,
> I have 10 vectors with 10 components, u(1:10,1:10).
> 
> And I am interested to calculated the differences between vectors  avoiding the diagonal components, because I am not interested in a high correlation between components.
> 
> This is my code, it works, but is not efficient, I would like to avoid the loops, but
> I do not know how I can do it:
> 
> for i=1:10
>    
>     for j=1:10
>         if j~=i 
>         dx=u(i,:)-u(j,:);
>         
>    if i==1 & j==2
>    delta=dx;
>    else 
>        delta=[delta;dx];
>    end
>         end
>         end
> 
> Please, anyone can help me to try to built the delta correlation matrix of another form more efficient.
> Thanks in advance,
> Jose.

  You can also do it this way:

 n = size(u,1);
 [I,J] = meshgrid(1:n,1:n);
 p = 1+(n+1)*(0:n-1);
 I(p) = []; J(p) = [];
 delta2 = u(I,:)-u(J,:);

Roger Stafford