Filter löschen
Filter löschen

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

3 Ansichten (letzte 30 Tage)
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

Antworten (1)

Rik
Rik am 25 Feb. 2018
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 Kommentare
Sanaz Kb
Sanaz Kb am 26 Feb. 2018
Bearbeitet: Sanaz Kb am 1 Mär. 2018
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
Rik
Rik am 9 Mär. 2018
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.

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by