Path: news.mathworks.com!not-for-mail From: <HIDDEN> Newsgroups: comp.soft-sys.matlab Subject: Re: Vectorize Difference Between Two Sets of Points Date: Mon, 26 Jan 2009 22:29:03 +0000 (UTC) Organization: Xoran Technologies Lines: 48 Message-ID: <glldff$m5s$1@fred.mathworks.com> References: <43737d62-9a77-4c67-93d1-8f4112930c99@x16g2000prn.googlegroups.com> <c646b7d0-a6ca-4a62-a0db-983f49e361ca@z6g2000pre.googlegroups.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 1233008943 22716 172.30.248.37 (26 Jan 2009 22:29:03 GMT) X-Complaints-To: news@mathworks.com NNTP-Posting-Date: Mon, 26 Jan 2009 22:29:03 +0000 (UTC) X-Newsreader: MATLAB Central Newsreader 1440443 Xref: news.mathworks.com comp.soft-sys.matlab:514106 NZTideMan <mulgor@gmail.com> wrote in message <c646b7d0-a6ca-4a62-a0db-983f49e361ca@z6g2000pre.googlegroups.com>... > On Jan 27, 10:51=A0am, Bill Woessner <woess...@gmail.com> wrote: > > I'm sure this question has been asked and answered dozens of times. > > But for the life of me, I can't find the answer. > > > > I have two sets of 2D points, call them A and B. =A0They're stored in > > matrices such that size(A) =3D [M 2] and size(B) =3D [N 2]. =A0For each a= > in > > A and b in B, I need to compute a - b. =A0This is painfully slow using > > for loops. =A0I've been trying to use repmat to speed it up, but so > > far... it's hideous: > > > > M =3D length(A); > > N =3D length(B); > > > > A2 =3D zeros(M, 1, 2); > > A2(:, 1, :) =3D A; > > A2 =3D repmat(A2, [1 N 1]); > > > > B2 =3D zeros(1, N, 2); > > B2(1, :, :) =3D B; > > B2 =3D repmat(B2, [M 1 1]); > > > > delta =3D A2 - B2; > > > > A and B are relatively small so memory is not a concern. =A0But speed > > is. =A0I know there's probably a better way to accomplish this. =A0Can > > someone please point me in the right direction? > > > > Thanks in advance, > > Bill Woessner > > How about this: > % Convert to complex vectors > Ac=3DA(:,1) + i*A(:,2); > Bc=3DB(:,1) + i*B(:,2); > % Allocate storage > D=3Dzeros(M,N); > for ib=3D1:N > D(:,ib)=3DAc - Bc(ib); > end You can avoid the for-loop altogether using bsxfun Ac=A(:,1) + i*A(:,2); Bc=B(:,1) + i*B(:,2); Bc=Bc.'; D=bsxfun(@minus, Ac,Bc);