Path: news.mathworks.com!not-for-mail From: "Bruno Luong" <b.luong@fogale.findmycountry> Newsgroups: comp.soft-sys.matlab Subject: Re: Shortest distance from Matrix to a line segment Date: Mon, 15 Aug 2011 21:45:29 +0000 (UTC) Organization: FOGALE nanotech Lines: 37 Message-ID: <j2c41p$6hq$1@newscl01ah.mathworks.com> References: <j2bu2g$ghi$1@newscl01ah.mathworks.com> Reply-To: "Bruno Luong" <b.luong@fogale.findmycountry> NNTP-Posting-Host: www-06-blr.mathworks.com Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Trace: newscl01ah.mathworks.com 1313444729 6714 172.30.248.38 (15 Aug 2011 21:45:29 GMT) X-Complaints-To: news@mathworks.com NNTP-Posting-Date: Mon, 15 Aug 2011 21:45:29 +0000 (UTC) X-Newsreader: MATLAB Central Newsreader 390839 Xref: news.mathworks.com comp.soft-sys.matlab:740089 "James D" <j.e.daniell@gmail.com> wrote in message <j2bu2g$ghi$1@newscl01ah.mathworks.com>... > Dear all, > I have a problem that I am trying to solve. > > I have a matrix of 600 x 840 different positions of latitude and longitude and I am trying to find and output the shortest distance to a line segment. > > I have created the following for the distance to a point and vectorised it so that it works faster than a for statement. > > I was wondering how I would code the shortest distance to a particular line segment for each of the 600 x 840 points in the matrix. > > distancematrix=sqrt( bsxfun(@plus, ((yllcorner+600*spacing-0.020833333:-spacing:yllcorner+0.020833333).'-centrallat).^2 ,... > ((xllcorner+0.020833333:spacing:xllcorner+840*spacing-0.020833333)-centrallong).^2 ) ); > > I hope that you can help! > Many thanks in advance, > James. I assume the rectangle is (long, lat) such that left <= long <=right (eqt1) bottom <= lat <=top (eqt2). The line segment in its parametric form is (x,t) x(t) = x1 + t*(x2-x1) y(t) = y1 + t*(y2-y1) 0 <= t <= 1 (eqt3). The distance squared is d2 d2 = (x(t)-long)^2 + (y(t)-lat)^2. It is clear that d2 is a quadratic form with respect to (3 x 1) vector [long,lat,t]'. The problem is minimize d2 such that box constraints (1-3). This can be solved with QUADPROG. There may be some function in FEX that can solve such problem. Bruno