Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
creating distance matrix

Subject: creating distance matrix

From: Yoav Rubin

Date: 29 May, 2009 07:30:04

Message: 1 of 6

I have two vectors, A and B, and I'd like to create a distance matrix C, that's its dims are numel(A) x numel(B), and C(i,j) is A(i) - B(j). It is simple to do it with loops, but I'd like to optimize it, as A and B are huge, and this calculation repeats many times.
Is there a way to do it fast, without loops?

thanks

Yoav

Subject: creating distance matrix

From: Gavrilo Bozovic

Date: 29 May, 2009 07:38:01

Message: 2 of 6

"Yoav Rubin" <yoavrubin@hotmail.com> wrote in message <gvo2ts$e2g$1@fred.mathworks.com>...
> I have two vectors, A and B, and I'd like to create a distance matrix C, that's its dims are numel(A) x numel(B), and C(i,j) is A(i) - B(j). It is simple to do it with loops, but I'd like to optimize it, as A and B are huge, and this calculation repeats many times.
> Is there a way to do it fast, without loops?
>
> thanks
>
> Yoav

in two lines:

[AA BB] = meshgrid(A,B);
C = AA-BB;

cheers

Subject: creating distance matrix

From: John D'Errico

Date: 29 May, 2009 09:08:01

Message: 3 of 6

"Yoav Rubin" <yoavrubin@hotmail.com> wrote in message <gvo2ts$e2g$1@fred.mathworks.com>...
> I have two vectors, A and B, and I'd like to create a distance matrix C, that's its dims are numel(A) x numel(B), and C(i,j) is A(i) - B(j). It is simple to do it with loops, but I'd like to optimize it, as A and B are huge, and this calculation repeats many times.
> Is there a way to do it fast, without loops?
>

If A and B are huge, then you probably don't have
the memory to store the distance matrix. ;-)

The BEST way to compute a distance matrix is
with bsxfun. Assume that A and B are vectors.
(I'll assume row vectors here.)

  C = abs(bsxfun(@minus,A',B));

See that I've taken the absolute value, to make the
result a valid distance. If you really wanted the
simple difference of A and B, then drop the abs.

John

Subject: creating distance matrix

From: Gavrilo Bozovic

Date: 29 May, 2009 13:53:01

Message: 4 of 6

"John D'Errico" <woodchips@rochester.rr.com> wrote in message <gvo8lh$ogo$1@fred.mathworks.com>...
> "Yoav Rubin" <yoavrubin@hotmail.com> wrote in message <gvo2ts$e2g$1@fred.mathworks.com>...
> > I have two vectors, A and B, and I'd like to create a distance matrix C, that's its dims are numel(A) x numel(B), and C(i,j) is A(i) - B(j). It is simple to do it with loops, but I'd like to optimize it, as A and B are huge, and this calculation repeats many times.
> > Is there a way to do it fast, without loops?
> >
>
> If A and B are huge, then you probably don't have
> the memory to store the distance matrix. ;-)
>
> The BEST way to compute a distance matrix is
> with bsxfun. Assume that A and B are vectors.
> (I'll assume row vectors here.)
>
> C = abs(bsxfun(@minus,A',B));
>
> See that I've taken the absolute value, to make the
> result a valid distance. If you really wanted the
> simple difference of A and B, then drop the abs.
>
> John

And I thought my solution was elegant... ;-)

Subject: creating distance matrix

From: someone

Date: 29 May, 2009 15:24:02

Message: 5 of 6

"Gavrilo Bozovic" <gavrilo.dot.bozovic@gmail.dot.ch> wrote in message <gvopbt$fkt$1@fred.mathworks.com>...
> "John D'Errico" <woodchips@rochester.rr.com> wrote in message <gvo8lh$ogo$1@fred.mathworks.com>...
> > "Yoav Rubin" <yoavrubin@hotmail.com> wrote in message <gvo2ts$e2g$1@fred.mathworks.com>...
> > > I have two vectors, A and B, and I'd like to create a distance matrix C, that's its dims are numel(A) x numel(B), and C(i,j) is A(i) - B(j). It is simple to do it with loops, but I'd like to optimize it, as A and B are huge, and this calculation repeats many times.
> > > Is there a way to do it fast, without loops?
> > >
> >
> > If A and B are huge, then you probably don't have
> > the memory to store the distance matrix. ;-)
> >
> > The BEST way to compute a distance matrix is
> > with bsxfun. Assume that A and B are vectors.
> > (I'll assume row vectors here.)
> >
> > C = abs(bsxfun(@minus,A',B));
> >
> > See that I've taken the absolute value, to make the
> > result a valid distance. If you really wanted the
> > simple difference of A and B, then drop the abs.
> >
> > John
>
> And I thought my solution was elegant... ;-)

Elegant, yes.
But there is a difference between elegant and optimum.
Also, "optimize it" does not necessarily mean "without loops" (in some cases).

Subject: creating distance matrix

From: Yoav Rubin

Date: 29 May, 2009 18:25:03

Message: 6 of 6

thanks, the bsxfun solved it perfectly
"someone" <someone@somewhere.net> wrote in message <gvoumi$jtp$1@fred.mathworks.com>...
> "Gavrilo Bozovic" <gavrilo.dot.bozovic@gmail.dot.ch> wrote in message <gvopbt$fkt$1@fred.mathworks.com>...
> > "John D'Errico" <woodchips@rochester.rr.com> wrote in message <gvo8lh$ogo$1@fred.mathworks.com>...
> > > "Yoav Rubin" <yoavrubin@hotmail.com> wrote in message <gvo2ts$e2g$1@fred.mathworks.com>...
> > > > I have two vectors, A and B, and I'd like to create a distance matrix C, that's its dims are numel(A) x numel(B), and C(i,j) is A(i) - B(j). It is simple to do it with loops, but I'd like to optimize it, as A and B are huge, and this calculation repeats many times.
> > > > Is there a way to do it fast, without loops?
> > > >
> > >
> > > If A and B are huge, then you probably don't have
> > > the memory to store the distance matrix. ;-)
> > >
> > > The BEST way to compute a distance matrix is
> > > with bsxfun. Assume that A and B are vectors.
> > > (I'll assume row vectors here.)
> > >
> > > C = abs(bsxfun(@minus,A',B));
> > >
> > > See that I've taken the absolute value, to make the
> > > result a valid distance. If you really wanted the
> > > simple difference of A and B, then drop the abs.
> > >
> > > John
> >
> > And I thought my solution was elegant... ;-)
>
> Elegant, yes.
> But there is a difference between elegant and optimum.
> Also, "optimize it" does not necessarily mean "without loops" (in some cases).

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us