Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Calculate distance between many points and one certain point
Date: Wed, 18 Aug 2010 19:40:31 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 26
Message-ID: <i4hcvf$t69$1@fred.mathworks.com>
References: <i4hbuo$fd2$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-03-blr.mathworks.com
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1282160431 29897 172.30.248.38 (18 Aug 2010 19:40:31 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Wed, 18 Aug 2010 19:40:31 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1895050
Xref: news.mathworks.com comp.soft-sys.matlab:663154

"JohnMC " <nirmathink@gmail.com> wrote in message <i4hbuo$fd2$1@fred.mathworks.com>...
> I have a large matrix, I want to calculate the distance of each point of the matrix from one certain point which is also in the matrix. 
> 
> if I used a loop, 
> 
> for i= 1:m
>    for j= 1:n
>   dist(i,j) = sqrt((i-x0)^2 + (j-y0)^2));
> end
> end
> 
> Is there any fast way to do this, because the matrix I am processing is large. 
> 
> Thanks a lot.

A few things:
1) Don't use i and j as your index variables, as these are both used for the complex unit.
2) Your loop does not use points given in a matrix.  Rather it finds the distance from your point (x0,y0) to each integer point of the form (i,j) for 1<=i<=m and 1<=j<=n.  This doesn't sound like your intent.
3) How are your points stored?  I will assume they are in an nx2 matrix, where the first column is the x-value and the second column is the y-value.
4) sqrt is vectorized, so, under the assumption from (3), the following will work:

n=100; % number of points
A=rand(n,2); % random data
x0 = 0.5;
y0 = 0.5; % a fixed point of interest
distances = sqrt((A(:,1)-x0).^2 + (A(:,2)-y0).^2);