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:39:02 +0000 (UTC) Organization: The MathWorks, Inc. Lines: 35 Message-ID: <glle26$2k9$1@fred.mathworks.com> References: <43737d62-9a77-4c67-93d1-8f4112930c99@x16g2000prn.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 1233009542 2697 172.30.248.37 (26 Jan 2009 22:39:02 GMT) X-Complaints-To: news@mathworks.com NNTP-Posting-Date: Mon, 26 Jan 2009 22:39:02 +0000 (UTC) X-Newsreader: MATLAB Central Newsreader 1187260 Xref: news.mathworks.com comp.soft-sys.matlab:514113 Bill Woessner <woessner@gmail.com> wrote in message <43737d62-9a77-4c67-93d1-8f4112930c99@x16g2000prn.googlegroups.com>... > 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. They're stored in > matrices such that size(A) = [M 2] and size(B) = [N 2]. For each a in > A and b in B, I need to compute a - b. This is painfully slow using > for loops. I've been trying to use repmat to speed it up, but so > far... it's hideous: > > M = length(A); > N = length(B); > > A2 = zeros(M, 1, 2); > A2(:, 1, :) = A; > A2 = repmat(A2, [1 N 1]); > > B2 = zeros(1, N, 2); > B2(1, :, :) = B; > B2 = repmat(B2, [M 1 1]); > > delta = A2 - B2; > > A and B are relatively small so memory is not a concern. But speed > is. I know there's probably a better way to accomplish this. Can > someone please point me in the right direction? > > Thanks in advance, > Bill Woessner [I,J] = ndgrid(1:M,1:N); delta = A(I,:)-B(J,:); Roger Stafford