[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Vectorizing loops
From: |
andrewcd |
Subject: |
Re: Vectorizing loops |
Date: |
Thu, 12 Jan 2012 11:59:57 -0800 (PST) |
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
--
View this message in context:
http://octave.1599824.n4.nabble.com/Vectorizing-loops-tp3992596p4290253.html
Sent from the Octave - General mailing list archive at Nabble.com.
- Re: Vectorizing loops,
andrewcd <=