help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Vectorizing loops


From: Ozzy Lash
Subject: Re: Vectorizing loops
Date: Thu, 12 Jan 2012 14:19:18 -0600

On Thu, Jan 12, 2012 at 1:59 PM, andrewcd <address@hidden> wrote:
> Bumping an old thread of mine with a new (similar) question.  I hope I'm not
> being a pest, but I'm an occasional user at best -- I mostly work in stata,
> but occasionally I need to do simulations (or huge-N stats).
>
> My problem:  I'm trying to simulate diffusion of a thing in space, and to do
> that I need to calculate the Euclidian distance between all of the nodes.  I
> have it below in loop format.  it is slow enough with 1000 units, but I want
> to bring it to 1 million once I get the rest of the code finished.  Any
> hints on how I could vectorize this particular loop?
>
> Many thanks, muchas gracias, asante sana.
>
> peeps=1000
>
>
> %Procedure
> %1) Define Space
> x = 100
> y = 100
> %2)Define units
>        folks = ones(peeps,1)
>        %-Give these folks coordinates
>        folks(:,2) = rand(peeps,1)*x;
>        folks(:,3) = rand(peeps,1)*y;
> %3) Define distances between dudes
> for i = 1:peeps
> for j = 1:peeps
>        dist(i,j) = (abs(folks(i,2)-folks(j,2)).^2 +
> abs(folks(i,3)-folks(j,3)).^2).^.5;
>        end end
>

One sanity check here.  For the case of 1000 peeps that you have here,
the dist() array will have 1000000 elements.  If, as you say, you plan
on moving the 1000000 peeps, the array would be 1000000 by 1000000, or
10^12 elements.  I don't think you'll be able to store that much data,
in memory, let alone process it in a timely fashion, no matter how
well vectorized it is.

Bill


reply via email to

[Prev in Thread] Current Thread [Next in Thread]