Path: news.mathworks.com!not-for-mail From: <HIDDEN> Newsgroups: comp.soft-sys.matlab Subject: Re: need help with distance of coordinates Date: Thu, 25 Nov 2010 04:30:05 +0000 (UTC) Organization: Idaho National Laboratory Lines: 39 Message-ID: <ickood$3pq$1@fred.mathworks.com> References: <icioug$2l4$1@fred.mathworks.com> <icjlbs$j4$1@fred.mathworks.com> <ickbrs$qhd$1@fred.mathworks.com> <a7006b27-31ef-4743-a746-c158340112e5@g19g2000yqg.googlegroups.com> Reply-To: <HIDDEN> NNTP-Posting-Host: webapp-02-blr.mathworks.com Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Trace: fred.mathworks.com 1290659405 3898 172.30.248.37 (25 Nov 2010 04:30:05 GMT) X-Complaints-To: news@mathworks.com NNTP-Posting-Date: Thu, 25 Nov 2010 04:30:05 +0000 (UTC) X-Newsreader: MATLAB Central Newsreader 688530 Xref: news.mathworks.com comp.soft-sys.matlab:689856 ImageAnalyst <imageanalyst@mailinator.com> wrote in message <a7006b27-31ef-4743-a746-c158340112e5@g19g2000yqg.googlegroups.com>... > 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 IA, I believe you need to start with row = 0, or move the increment to a place after the distance calculation. To the OP, if Roger's code causes a memory problem, you should use the IA's code (with the change mentioned above), and don't forget to Pre-allocate the distance matrix. I say this because you said that your actual data was much larger than the example. I.e., distance = zeros(length(x2)^2*length(x1)^2);