http://www.mathworks.com/matlabcentral/newsreader/view_thread/243140
MATLAB Central Newsreader  Minimum distances of one vector to another
Feed for thread: Minimum distances of one vector to another
enus
©19942015 by MathWorks, Inc.
webmaster@mathworks.com
MATLAB Central Newsreader
http://blogs.law.harvard.edu/tech/rss
60
MathWorks
http://www.mathworks.com/images/membrane_icon.gif

Mon, 26 Jan 2009 16:49:02 +0000
Minimum distances of one vector to another
http://www.mathworks.com/matlabcentral/newsreader/view_thread/243140#624033
Robert Kirdeikis
Hi, i have a program that collapses a sphere onto a body, the program works well but my next step is finding the furthest point away from the body to determine the error. the method i got in there is<br>
<br>
j=2562<br>
win=zeros(m,1);<br>
for j=1:m<br>
win(j)=min(sqrt(sum(bsxfun(@minus,data,sdata(j,:)).^2,2)));<br>
end<br>
max(win)<br>
<br>
where sdata is a 2562X3 matrix and data is a 52000X3 matrix. This program finds the minimum for every row of sdata and stores it in win then finds the maximum distance away from the body. This works okay but the for loop slows down my program by quite a bit and i am looking for suggestions on how to make my program more efficient. Any help would be appreciated and thanks in advance.

Mon, 26 Jan 2009 17:29:55 +0000
Re: Minimum distances of one vector to another
http://www.mathworks.com/matlabcentral/newsreader/view_thread/243140#624042
Walter Roberson
Robert Kirdeikis wrote:<br>
> Hi, i have a program that collapses a sphere onto a body, the program works well but my next<br>
> step is finding the furthest point away from the body to determine the error. the method i<br>
> got in there is<br>
<br>
> j=2562<br>
> win=zeros(m,1);<br>
> for j=1:m<br>
> win(j)=min(sqrt(sum(bsxfun(@minus,data,sdata(j,:)).^2,2)));<br>
> end<br>
> max(win)<br>
<br>
> where sdata is a 2562X3 matrix and data is a 52000X3 matrix. This program finds the<br>
> minimum for every row of sdata and stores it in win then finds the maximum distance<br>
> away from the body. This works okay but the for loop slows down my program by quite a<br>
> bit and i am looking for suggestions on how to make my program more efficient.<br>
<br>
Some small tweaking that might add up:<br>
<br>
min(sqrt(X(j))) is sqrt(min(X(j)))<br>
<br>
max(sqrt(min(X)) is sqrt(max(min(X))<br>
<br>
Thus you can eliminate all of the sqrt() calls except for one last trivial one.<br>
<br>
 <br>
.signature note: I am now avoiding replying to unclear or ambiguous postings.<br>
Please review questions before posting them. Be specific. Use examples of what you mean,<br>
of what you don't mean. Specify boundary conditions, and data classes and value<br>
relationships  what if we scrambled your data or used Inf, NaN, or complex(rand,rand)?

Mon, 26 Jan 2009 17:48:02 +0000
Re: Minimum distances of one vector to another
http://www.mathworks.com/matlabcentral/newsreader/view_thread/243140#624049
Robert Kirdeikis
Ive fixed the square root to perform the rooting outside the loop and it has saved a bit of time. Thank you for the suggestion. I am still looking for a built in matlab function or a matrix way to get rid of the for loop which is the main time consumer to find all the minimum distances in 3d of each point on my sdata vector to a point or surface made from my data vector

Mon, 26 Jan 2009 17:50:19 +0000
Re: Minimum distances of one vector to another
http://www.mathworks.com/matlabcentral/newsreader/view_thread/243140#624050
nor ki
"Robert Kirdeikis" <kirdeiki@ualberta.ca> wrote in message <glkphu$6v$1@fred.mathworks.com>...<br>
> Hi, i have a program that collapses a sphere onto a body, the program works well but my next step is finding the furthest point away from the body to determine the error. the method i got in there is<br>
> <br>
> j=2562<br>
> win=zeros(m,1);<br>
> for j=1:m<br>
> win(j)=min(sqrt(sum(bsxfun(@minus,data,sdata(j,:)).^2,2)));<br>
> end<br>
> max(win)<br>
> <br>
> where sdata is a 2562X3 matrix and data is a 52000X3 matrix. This program finds the minimum for every row of sdata and stores it in win then finds the maximum distance away from the body. This works okay but the for loop slows down my program by quite a bit and i am looking for suggestions on how to make my program more efficient. Any help would be appreciated and thanks in advance.<br>
<br>
Hi Robert,<br>
<br>
bsxfun seems to allocate memory on order to calculate then with two matrices of equal size.<br>
My test :<br>
clc<br>
A = magic(1000);<br>
m = mean(A);<br>
tic<br>
for n = 1:20<br>
AA = bsxfun(@minus, A, m);<br>
end<br>
toc<br>
<br>
tic<br>
for n = 1:20<br>
for n = size(m, 2):1:1<br>
AA2(:,n) = A(:,n) m(n);<br>
end<br>
end<br>
toc<br>
<br>
if you loop twice this should prevent unnecessary allocation<br>
<br>
j=2562<br>
win=zeros(m,1);<br>
for j=1:m<br>
<br>
win(j)=min(sqrt(sum(bsxfun(@minus,data,sdata(j,:)).^2,2)));<br>
end<br>
max(win)<br>
<br>
gives<br>
Elapsed time is 0.196413 seconds.<br>
Elapsed time is 0.123616 seconds.<br>
<br>
with Walters hint this looks rather like<br>
<br>
j=2562<br>
win=zeros(m,1);<br>
for j=1:m<br>
for n = size(sdata,2):1:1<br>
tmp(n) = datasdata(j,n);<br>
end<br>
win(j) = min(sum(tmp.^2));<br>
<br>
% win(j)=min(sqrt(sum(bsxfun(@minus,data,sdata(j,:)).^2,2)));<br>
end<br>
<br>
sqrt(max(win))<br>
<br>
hth kinor

Mon, 26 Jan 2009 17:55:03 +0000
Re: Minimum distances of one vector to another
http://www.mathworks.com/matlabcentral/newsreader/view_thread/243140#624053
nor ki
"Robert Kirdeikis" <kirdeiki@ualberta.ca> wrote in message <glkphu$6v$1@fred.mathworks.com>...<br>
> Hi, i have a program that collapses a sphere onto a body, the program works well but my next step is finding the furthest point away from the body to determine the error. the method i got in there is<br>
> <br>
> j=2562<br>
> win=zeros(m,1);<br>
> for j=1:m<br>
> win(j)=min(sqrt(sum(bsxfun(@minus,data,sdata(j,:)).^2,2)));<br>
> end<br>
> max(win)<br>
> <br>
> where sdata is a 2562X3 matrix and data is a 52000X3 matrix. This program finds the minimum for every row of sdata and stores it in win then finds the maximum distance away from the body. This works okay but the for loop slows down my program by quite a bit and i am looking for suggestions on how to make my program more efficient. Any help would be appreciated and thanks in advance.<br>
<br>
Hi Robert,<br>
<br>
bsxfun seems to allocate memory on order to calculate then with two matrices of equal size.<br>
My test :<br>
clc<br>
A = magic(1000);<br>
m = mean(A);<br>
tic<br>
for n = 1:20<br>
AA = bsxfun(@minus, A, m);<br>
end<br>
toc<br>
<br>
tic<br>
for n = 1:20<br>
for n = size(m, 2):1:1<br>
AA2(:,n) = A(:,n) m(n);<br>
end<br>
end<br>
toc<br>
<br>
if you loop twice this should prevent unnecessary allocation<br>
<br>
j=2562<br>
win=zeros(m,1);<br>
for j=1:m<br>
<br>
win(j)=min(sqrt(sum(bsxfun(@minus,data,sdata(j,:)).^2,2)));<br>
end<br>
max(win)<br>
<br>
gives<br>
Elapsed time is 0.196413 seconds.<br>
Elapsed time is 0.123616 seconds.<br>
<br>
with Walters hint this looks rather like<br>
<br>
j=2562<br>
win=zeros(m,1);<br>
for j=1:m<br>
for n = size(sdata,2):1:1<br>
tmp(n) = datasdata(j,n);<br>
end<br>
win(j) = min(sum(tmp.^2));<br>
<br>
% win(j)=min(sqrt(sum(bsxfun(@minus,data,sdata(j,:)).^2,2)));<br>
end<br>
<br>
sqrt(max(win))<br>
<br>
hth kinor

Mon, 26 Jan 2009 18:28:03 +0000
Re: Minimum distances of one vector to another
http://www.mathworks.com/matlabcentral/newsreader/view_thread/243140#624064
Robert Kirdeikis
Thank you hth kinor, the time required to run the loop went down from tElapsed=6.186647 seconds to tElapsed=5.004199 seconds on a smaller iteration loop i used to test it. If anybody has any more ideas on how to shave off some computation time, id appreciate hearing your ideas

Mon, 26 Jan 2009 19:28:05 +0000
Re: Minimum distances of one vector to another
http://www.mathworks.com/matlabcentral/newsreader/view_thread/243140#624079
ImageAnalyst
Robert:<br>
I can't figure out why you're taking the square root. If you want to<br>
find the closest or farthest distance, simply searching on the<br>
distance squared will do it. No need to take the square root until<br>
the search is done. I'm surprised no one else mentioned it  am I<br>
incorrect here?<br>
Regards,<br>
ImageAnalyst

Mon, 26 Jan 2009 19:39:01 +0000
Re: Minimum distances of one vector to another
http://www.mathworks.com/matlabcentral/newsreader/view_thread/243140#624082
us
ImageAnalyst<br>
> No need to take the square root until the search is done.<br>
> I'm surprised no one else mentioned it  am I<br>
> incorrect here...<br>
<br>
yes, walter (first reply) has mentioned this about 2hrs ago...<br>
<br>
:)<br>
us

Mon, 26 Jan 2009 19:39:02 +0000
Re: Minimum distances of one vector to another
http://www.mathworks.com/matlabcentral/newsreader/view_thread/243140#624083
Roger Stafford
ImageAnalyst <imageanalyst@mailinator.com> wrote in message <1fa8fad2d4a84ae2a74dd4c4cb1e827d@r36g2000prf.googlegroups.com>...<br>
> Robert:<br>
> I can't figure out why you're taking the square root. If you want to<br>
> find the closest or farthest distance, simply searching on the<br>
> distance squared will do it. No need to take the square root until<br>
> the search is done. I'm surprised no one else mentioned it  am I<br>
> incorrect here?<br>
> Regards,<br>
> ImageAnalyst<br>
<br>
Walter already mentioned that in this thread.<br>
<br>
Roger Stafford