i want to find distances between coordinate points as i explained below?? any help??

i have coordinates as
A = (0,0); B = (1,0); C = (2,0)
D = (0,1); E = (1,1); F = (2,1)
G = (0,2); H = (1,2); I = (2,2)
i want to distance between A to D plus D to B and so on......
matrix 1 (2*3)
[(AtoD+DtoB) (AtoE+EtoB) (AtoF+FtoB)
(AtoG+GtoB) (AtoH+HtoB) (AtoI+ItoB)
matrix 2 (2*3)
[(AtoD+DtoC) (AtoE+EtoC) (AtoF+FtoC)
(AtoG+GtoC) (AtoH+HtoC) (AtoI+ItoC)
matrix 3 (2*3)
[(BtoD+DtoA) (BtoE+EtoA) (BtoF+FtoA)
(BtoG+GtoA) (BtoH+HtoA) (BtoI+ItoA) ]
matrix 4 (2*3)
[(BtoD+DtoC) (BtoE+EtoC) (BtoF+FtoC)
(BtoG+GtoC) (BtoH+HtoC) (BtoI+ItoC) ]
matrix 5 (2*3)
[(CtoD+DtoA) (CtoE+EtoA) (CtoF+FtoA)
(CtoG+GtoA) (CtoH+HtoA) (CtoI+ItoA) ]
matrix 6 (2*3)
[(CtoD+DtoB) (CtoE+EtoB) (CtoF+FtoB)
(CtoG+GtoB) (CtoH+HtoB) (CtoI+ItoB) ]
finally i want six matrices as i explained above
thank you

6 Comments

no_gridsx = 3;
no_gridsy = 3;
no_receivers = no_gridsy-1;
step_x = 1; %spacing between grids 0.5 cm in x-direction
step_y = 1; %spacing between grids 0.5 cm in y-direction
[X,Y] = meshgrid(0:step_x:no_gridsy-1, 0:step_y:no_gridsx-1);
for kk = 1:no_receivers
for ii = 1:no_gridsx-1
for jj =1:no_gridsy
dist(ii,jj,kk) = sqrt((X(1,1)-X(ii+1,jj))^2+(Y(1,1)-Y(ii+1,jj))^2)+sqrt((X(ii+1,jj)-X(1,kk+1))^2+(Y(ii+1,jj)-Y(1,kk+1))^2)
end
end
end
I am able to get first two matrices ..
how to get next four matrices any help would be greatly appreciated
matrix 1 == matrix 3
matrix 2 == matrix 5
matrix 4 == matrix 6
What do you think? Does it make your task easier?

Sign in to comment.

 Accepted Answer

With use of pdist2()
A = [0 0]; B = [1 0]; C = [2 0];
[x,y] = meshgrid(0:2,1:2); % D G E H F I
D_I = [x(:) y(:)];
AtoXY = reshape(pdist2(A, D_I),size(x)); % matrix distance from A to (D G E H F I)
BtoXY = reshape(pdist2(B, D_I),size(x)); % distance from B
CtoXY = reshape(pdist2(C, D_I),size(x)); % distance from C
matrix1 = AtoXY + BtoXY;
matrix2 = AtoXY + CtoXY;
matrix4 = BtoXY + CtoXY;

13 Comments

sorry sir what u said is correct i did not notice it
thanks for ur answer
if i have 90*90 grid then i have to write so many lines right??
can we put that in to loop??
The scheme is the same? If a grid 90x90 then each matrix is 89x90? How many matrix will be then?
3x3 grid: 3 matrices
4x4 grid: 6 matrices
5x5 grid: 10 matrices
NxN grid: matrices
the y grid value of y is 90. so no. of matrices are 89+88+87+86+.......+1 = 4005 matrices
Can you please show how to calculate matrix for 4x4 grid?
i will explain with your code
A = [0 0]; B = [1 0]; C = [2 0];
[x,y] = meshgrid(0:2,1:2); % D G E H F I
D_I = [x(:) y(:)];
AtoXY = reshape(pdist2(A, D_I),size(x)); % matrix distance from A to (D G E H F I)
BtoXY = reshape(pdist2(B, D_I),size(x)); % distance from B
CtoXY = reshape(pdist2(C, D_I),size(x)); % distance from C
matrix1 = AtoXY + BtoXY;
matrix2 = AtoXY + CtoXY;
matrix4 = BtoXY + CtoXY;
  1. lets fix A (A to B via (x y))
from distance from A to every point in (x y) plus from every point in (x y) to B = matrix 1
2. fix A (A to C via (x y))
from distance from A to every point in (x y) plus from every point in (x y) to C = matrix 2
3. fix B (B to A via (x y))
from distance from B to every point in (x y) plus from every point in (x y) to A = matrix 3
3. fix B and C(B to C via (x y))
from distance from B to every point in (x y) plus from every point in (x y) to A = matrix 4
3. fix C (C to A via (x y))
from distance from B to every point in (x y) plus from every point in (x y) to A = matrix 5
3. fix C (C to B via (x y))
from distance from B to every point in (x y) plus from every point in (x y) to A = matrix 6
as u said
matrix 1 == matrix 3
matrix 2 == matrix 5
matrix 4 == matrix 6
when we fix A remaing points are B and C (SO WE GET TWO MATRICES)
when we fix B remaing points are A and C (SO WE GET TWO MATRICES)
when we fix C remaing points are A and B (SO WE GET TWO MATRICES)
How many matrices we'll get for 4x4 grid?
we will get total 20 matrices
(as per your logic we get 10 matrices)
4*4 grid means we will get 5 cordinates in x and y direction
IN a grid of 4*4
A = (0 0); B = (1 0) ; C=(2 0) ;D = (3 0); E = (4 0)
A1 = (0 1); B1 = (1 1) ; C1=(2 1) ;D1 = (3 1); E1 = (4 1)
A2 = (0 2); B2 = (1 2) ; C2=(2 2) ;D2 = (3 2); E2 = (4 2)
A3= (0 3); B3= (1 3) ; C3=(2 3) ;D3 = (3 3); E3= (4 3)
A4 = (0 4); B4 = (1 4) ; C4=(2 4) ;D4 = (3 4); E4 = (4 4)
here A1 to E4 are x and y coordinates as u defined in ur code
from A to all x,y coordinates and from x,y coordinates to B we get matrix 1
from A to all x,y coordinates and from x,y coordinates to C we get matrix 2
from A to all x,y coordinates and from x,y coordinates to C we get matrix 3
from A to all x,y coordinates and from x,y coordinates to D we get matrix 4
from A to all x,y coordinates and from x,y coordinates to E we get matrix 5
I explained for A to
similarly for B,C,D,E
It should work
clc,clear
n = 4;
AA = [0:(n-1); zeros(1,n)]'; % top row: A B C ...
[x,y] = meshgrid(0:n-1,1:n-1); % XY grid
XY = [x(:) y(:)]; % make vector
D = pdist2(AA,XY);
% AtoXY = reshape(D(1,:),size(x))
% BtoXY = reshape(D(2,:),size(x))
% CtoXY = reshape(D(3,:),size(x))
% ...
k = 0;
matrix = cell((n-1)*n/2,1);
% get every possible combination
% [1 2], [1 3], [1 4] ...
% [2 3], [2 4] ...
% ...
for i = 1:n-1
for j = i+1:n
k = k + 1;
d = D(i,:)+D(j,:);
matrix{k} = reshape(d,size(x));
end
end
sir,
thank your very much for your kind reply. you were replying very patiently
code is working perfectly

Sign in to comment.

More Answers (0)

Asked:

on 7 Aug 2019

Commented:

on 10 Aug 2019

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!