how to calculate the distances between points coordinates and the arbitrary reference point using loops?

This solution is solved before within a little change, They have calculated the distances between pairwise coordinate. as you can see inside the link below:
But in my question the X and Y have points inside which are increase sequentially. For example:
Let us consider:
X = [0 2 4 6; 0 2 4 6; 0 2 4 6]; % and
Y = [0 0 0 0; 2 2 2 2; 4 4 4 4; 6 6 6 6];
all of the points inside X and Y are due to the geometry of my model configuration. Let consider a reference point which is inside the model.I need to find the distance between all points and reference point. I know that I should use a loop for the sequential changes but I don't know how?
I should note that the configuration may be changed.
Thank you
Sanaz

Answers (1)

If X and Y contain the coordinates of your sample points, then the code below will give you the Euclidean distance between the fixed point and the sample points.
F_x=3;F_y=3;%coordinates of the fixed point
E_distance=sqrt((X-F_x).^2+(Y-F_y).^2);
I don't really see the connection to the question you linked to, so please leave a comment if this isn't what you mean.

8 Comments

Many thanks for your answer.
X = [0 2 4 6 8 ...... 2*n]; %X coordinate of the model
Y = [0; 2; 4; 6; 8; ......; 2*m]; %Y coordinate of the model
F_x=3; %coordinates of the fixed point(arbitrary)
F_y=3; %coordinates of the fixed point(arbitrary)
Distance between the points and the reference(fixed) point in matrix form:
Dis_nm=sqrt((X(n*m)-F_x).^2+(Y(n*m)_y).^2);
B = [Dis_11 Dis_12 Dis_13 ......... Dis_1n;
Dis_21 Dis_22 Dis_23 ................ Dis_1n;
.
.
Dis_m1 Dis_m2 Dis_m3 ............. Dis_mn]
I have sketched it also by hand. (I don't know whether forbidden here or not).
I hope that the question would be clear.
Thank you Sanaz
What you are looking for is exactly what my code should do. As Star Strider suggested, you can also use the hypot function:
F_x=3;F_y=3;%coordinates of the fixed point
E_distance=hypot(X-F_x,Y-F_y);
Sanaz, if you need another opinion, then I'll add mine. The solution is just as Rik said before you posted your "clarifying" image. Did you even try it? If not, why not. If so, did something not go as expected?
However I admit I'm confused when you say
X = [0 2 4 6; 0 2 4 6; 0 2 4 6]; % and
Y = [0 0 0 0; 2 2 2 2; 4 4 4 4; 6 6 6 6];
so that X is a 3 row by 4 column matrix while Y is a 4 row by 4 column matrix. Exactly what are the "matched up" (x,y) coordinate pairs in that kind of non-equal size matrix layout????
And, once you find all the distances, what do you want to do? Find the min or max distance? Something else?
Many thanks for you both to answer my question and sorry if it was not clear. Yes I know they have not the same dimension. In my model any configuration must be considered. and I have given this example to know that how can I solve it when X and Y don't have the same dimension?, However, I need to put X and Y in loop which is considered the sequential changes n=(0 2 4 ... 2*i), I tried in my Matlab but I can't do it. after that I should consider a reference point and find the distances between the points (x,y) and the reference point.
This configuration is related to the pile as a foundation of the buildings.(I need to find the stresses and the strains which is applying on the piles.)
If you need to consider every combination, why don't the lengths match up like this then:
X = [0 2 4 6; 0 2 4 6; 0 2 4 6; 0 2 4 6];
Y = [0 0 0 0; 2 2 2 2; 4 4 4 4; 6 6 6 6];
Dear all thank you in advance for your useful answers. Now, I have found my solution by myself. In below I put the code and I hope it would be helpful for the users.
clc clear all format long
pos_x = 4; % number of the position in x coordinate
pos_y = 3; % number of the position in y coordinate
u = []
v = []
for i = 1:pos_x
u = [u, 2*(i-1)];
X = repmat(u,pos_y,1).';
v = [v,2*(i-1)];
Y = repmat(v,pos_y,1).';
end
n = size(X,2);
m = size(Y,2);
D = zeros(n,m);
xo = 0.5; % reference point x coordiante
yo = 0.5; % reference point y coordinate
for ii = 1:n
for jj = 1:m
D(ii,jj) = sum(sqrt((xo-X(ii)).^2 + (yo-Y(jj)).^2));
end
end
You can optimize your code like this:
pos_x = 4; % number of the position in x coordinate
pos_y = 3; % number of the position in y coordinate
u = [];
v = [];
for i = 1:pos_x
u = [u, 2*(i-1)];
X = repmat(u,pos_y,1).';
v = [v,2*(i-1)];
Y = repmat(v,pos_y,1).';
end
n = size(X,2);
m = size(Y,2);
D = zeros(n,m);
xo = 0.5; % reference point x coordiante
yo = 0.5; % reference point y coordinate
x_elem=X(1:n);
y_elem=Y(1:m);
[X2,Y2]=meshgrid(x_elem,y_elem);
D=hypot(xo-X2,yo-Y2);
But are you sure you don't mean something like the code below? Because your code results in a D that is not described by pos_x or pos_y.
pos_x = 4; % number of the position in x coordinate
pos_y = 3; % number of the position in y coordinate
xo = 0.5; % reference point x coordiante
yo = 0.5; % reference point y coordinate
u=0:2:(2*(pos_x-1));
v=0:2:(2*(pos_y-1));
[X,Y]=meshgrid(u,v);
D=hypot(xo-X,yo-Y);
If this solves your question, please consider marking it as accepted answer.

Sign in to comment.

Asked:

on 25 Feb 2018

Commented:

Rik
on 9 Mar 2018

Community Treasure Hunt

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

Start Hunting!