Path: news.mathworks.com!newsfeed-00.mathworks.com!newsfeed2.dallas1.level3.net!news.level3.com!postnews.google.com!g19g2000yqg.googlegroups.com!not-for-mail
From: ImageAnalyst <imageanalyst@mailinator.com>
Newsgroups: comp.soft-sys.matlab
Subject: Re: need help with distance of coordinates
Date: Wed, 24 Nov 2010 18:57:47 -0800 (PST)
Organization: http://groups.google.com
Lines: 40
Message-ID: <a7006b27-31ef-4743-a746-c158340112e5@g19g2000yqg.googlegroups.com>
References: <icioug$2l4$1@fred.mathworks.com> <icjlbs$j4$1@fred.mathworks.com> <ickbrs$qhd$1@fred.mathworks.com>
NNTP-Posting-Host: 184.57.134.178
Mime-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
X-Trace: posting.google.com 1290653867 30453 127.0.0.1 (25 Nov 2010 02:57:47 GMT)
X-Complaints-To: groups-abuse@google.com
NNTP-Posting-Date: Thu, 25 Nov 2010 02:57:47 +0000 (UTC)
Complaints-To: groups-abuse@google.com
Injection-Info: g19g2000yqg.googlegroups.com; posting-host=184.57.134.178; posting-account=0rLUzAkAAABojYSRC64DkTbtiSCX77HH
User-Agent: G2/1.0
X-HTTP-UserAgent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12)
 Gecko/20101026 Firefox/3.6.12,gzip(gfe)
Xref: news.mathworks.com comp.soft-sys.matlab:689848

So the obvious way is to just put your formula into a group of 4
nested for loops.  There may be some more efficient way but for less
than a few million numbers in your matrix, I doubt you'd notice any
difference.

x1=[1 2 3]
y1=[1 2 3]
x2=[2 4 6 8]
y2=[2 4 6 8]

row = 1;
col = 1;
for xx1 = x1
	for yy1 = y1
		for xx2=x2
			for yy2 = y2
				row = row+1;
				distance(row, col) = sqrt((xx2-xx1)^2+(yy2-yy1)^2);
				fprintf(1, 'From (%d, %d) to (%d, %d) = %.2f\n', ...
					xx1,yy1,xx2,yy2, distance(row, col));
				if row == length(x2)*length(y2)
					row = 0;
					col = col+1;
				end
			end
		end
	end
	row = 0;
end
% Display the final matrix.
distance


There is no way to remove values from the matrix.  A rectangular
matrix must have values in every location.  If you really want to do
that you'll have to abandon using a numerical matrix and switch to
using a cell array.  It will still have locations but the cells at
those locations could contain a null if you want.  Or maybe you just
want to do logical indexing, like in the FAQ:
http://matlab.wikia.com/wiki/FAQ#How_does_logical_indexing_work.3F