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 19:51:02 +0000 (UTC) Organization: Univ of Newcastle upon Tyne Lines: 81 Message-ID: <ghc0n6$a98$1@fred.mathworks.com> References: <ghbdre$ahh$1@fred.mathworks.com> Reply-To: <HIDDEN> NNTP-Posting-Host: webapp-05-blr.mathworks.com Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 8bit X-Trace: fred.mathworks.com 1228506662 10536 172.30.248.35 (5 Dec 2008 19:51:02 GMT) X-Complaints-To: news@mathworks.com NNTP-Posting-Date: Fri, 5 Dec 2008 19:51:02 +0000 (UTC) X-Newsreader: MATLAB Central Newsreader 1511480 Xref: news.mathworks.com comp.soft-sys.matlab:505274 Please, Matt, can yu written down the code complete for the matrix u? I spent all the day try to vectorizing the matrix avoiding loops. Here you are. u=[1 2 3; 4 5 6; 6 7 8] clear all; clc; u=[1 2 3; 4 5 6; 6 7 8] tic nc=size(u,2); delta=zeros(0,nc); %pre-llocated delta nt=length(u(:,1)) for i=1:nt for j=1:nt dx=u(i,:)-u(j,:); delta=[delta;dx]; end end delta toc 0 0 0 -3 -3 -3 -5 -5 -5 3 3 3 0 0 0 -2 -2 -2 5 5 5 2 2 2 0 0 0 You can send me your code, where you avoid the j-loop? I do not need the diagonal. Thanks in advance, Jose. "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.