Got Questions? Get Answers.
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:
caculating distance matrix

Subject: caculating distance matrix

From: doron bartov

Date: 22 Mar, 2012 10:25:17

Message: 1 of 5

hi,

i am dealing with a simulation of a large number of pirticles (around 10,000) and i need to calculate the distance between each one of them (all couples), my data is in the form of Nx3 matrix.
what i came up with is using meshgrid on each column of the position matrix thus getting the distance in each axis in three matrices. then i sumed the squared matrices to get the total distance matrix.
my problem is that when appling this code on a large number of particles memmory issues arise and the run is not completed

any idea?
Doron

Subject: caculating distance matrix

From: Torsten

Date: 22 Mar, 2012 11:06:05

Message: 2 of 5

Am Donnerstag, 22. März 2012 11:25:17 UTC+1 schrieb doron bartov:
> hi,
>
> i am dealing with a simulation of a large number of pirticles (around 10,000) and i need to calculate the distance between each one of them (all couples), my data is in the form of Nx3 matrix.
> what i came up with is using meshgrid on each column of the position matrix thus getting the distance in each axis in three matrices. then i sumed the squared matrices to get the total distance matrix.
> my problem is that when appling this code on a large number of particles memmory issues arise and the run is not completed
>
> any idea?
> Doron

Sure. Don't calculate the distances all at once, but calculate
the distance between particle i and j when this distance is needed.
It should not be such a big problem if the same distance calculation has
to be done several times in this way.

Best wishes
Torsten.

 

Subject: caculating distance matrix

From: John D'Errico

Date: 22 Mar, 2012 11:06:19

Message: 3 of 5

"doron bartov" <doronbartov@gmail.com> wrote in message <jkeumc$59l$1@newscl01ah.mathworks.com>...
> hi,
>
> i am dealing with a simulation of a large number of pirticles (around 10,000) and i need to calculate the distance between each one of them (all couples), my data is in the form of Nx3 matrix.
> what i came up with is using meshgrid on each column of the position matrix thus getting the distance in each axis in three matrices. then i sumed the squared matrices to get the total distance matrix.
> my problem is that when appling this code on a large number of particles memmory issues arise and the run is not completed
>
> any idea?

Ideas? You want to compute a 10,000 x 10,000 matrix.
There are 1e8 elements in that matrix, taking up 8e8
bytes of memory to store as doubles. 800 megabytes.
Don't make many copies of that array.

You are running out of memory, since windows greatly
limits the memory you have available.

You can save half of that, by computing it as a single
array. Or use a 64 bit version of matlab, and buy more
memory.

John

Subject: caculating distance matrix

From: doron bartov

Date: 22 Mar, 2012 11:24:16

Message: 4 of 5

Torsten <Torsten.Hennig@umsicht.fraunhofer.de> wrote in message <1373985.906.1332414365420.JavaMail.geo-discussion-forums@vbut24>...
> Am Donnerstag, 22. März 2012 11:25:17 UTC+1 schrieb doron bartov:
> > hi,
> >
> > i am dealing with a simulation of a large number of pirticles (around 10,000) and i need to calculate the distance between each one of them (all couples), my data is in the form of Nx3 matrix.
> > what i came up with is using meshgrid on each column of the position matrix thus getting the distance in each axis in three matrices. then i sumed the squared matrices to get the total distance matrix.
> > my problem is that when appling this code on a large number of particles memmory issues arise and the run is not completed
> >
> > any idea?
> > Doron
>
> Sure. Don't calculate the distances all at once, but calculate
> the distance between particle i and j when this distance is needed.
> It should not be such a big problem if the same distance calculation has
> to be done several times in this way.
>
> Best wishes
> Torsten.
>
> hi and thanks Torsten,

the problem is that i need to use the distance for various other variables, one of them is inversially propotional to the distances and this takes him a very long time to calculate.
thanks,
Doron

Subject: caculating distance matrix

From: doron bartov

Date: 22 Mar, 2012 11:28:18

Message: 5 of 5

"John D'Errico" <woodchips@rochester.rr.com> wrote in message <jkf13b$c43$1@newscl01ah.mathworks.com>...
> "doron bartov" <doronbartov@gmail.com> wrote in message <jkeumc$59l$1@newscl01ah.mathworks.com>...
> > hi,
> >
> > i am dealing with a simulation of a large number of pirticles (around 10,000) and i need to calculate the distance between each one of them (all couples), my data is in the form of Nx3 matrix.
> > what i came up with is using meshgrid on each column of the position matrix thus getting the distance in each axis in three matrices. then i sumed the squared matrices to get the total distance matrix.
> > my problem is that when appling this code on a large number of particles memmory issues arise and the run is not completed
> >
> > any idea?
>
> Ideas? You want to compute a 10,000 x 10,000 matrix.
> There are 1e8 elements in that matrix, taking up 8e8
> bytes of memory to store as doubles. 800 megabytes.
> Don't make many copies of that array.
>
> You are running out of memory, since windows greatly
> limits the memory you have available.
>
> You can save half of that, by computing it as a single
> array. Or use a 64 bit version of matlab, and buy more
> memory.
>
> John

Hi John,

do you mean not calculate x y and z distances separately? that is, concentrate them in the same array ?

Tags for 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